Skip to content

Commit ddefb53

Browse files
authored
fix: email verified in login methods (#171)
* fix: email verified in login methods * fix: version update * fix: pr comments
1 parent 45f662c commit ddefb53

File tree

4 files changed

+106
-23
lines changed

4 files changed

+106
-23
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
99

10+
## [5.0.2] - 2023-11-01
11+
12+
- Fixes `verified` in `loginMethods` for users with userId mapping
13+
1014
## [5.0.1] - 2023-10-12
1115

1216
- Fixes user info from primary user id query

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ plugins {
22
id 'java-library'
33
}
44

5-
version = "5.0.1"
5+
version = "5.0.2"
66

77
repositories {
88
mavenCentral()

src/main/java/io/supertokens/storage/postgresql/queries/EmailVerificationQueries.java

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -259,26 +259,50 @@ public static List<String> isEmailVerified_transaction(Start start, Connection s
259259
return new ArrayList<>();
260260
}
261261
List<String> emails = new ArrayList<>();
262-
List<String> userIds = new ArrayList<>();
263-
Map<String, String> userIdToEmailMap = new HashMap<>();
262+
List<String> supertokensUserIds = new ArrayList<>();
264263
for (UserIdAndEmail ue : userIdAndEmail) {
265264
emails.add(ue.email);
266-
userIds.add(ue.userId);
265+
supertokensUserIds.add(ue.userId);
267266
}
267+
268+
// We have external user id stored in the email verification table, so we need to fetch the mapped userids for
269+
// calculating the verified emails
270+
271+
HashMap<String, String> supertokensUserIdToExternalUserIdMap = UserIdMappingQueries.getUserIdMappingWithUserIds_Transaction(start,
272+
sqlCon, supertokensUserIds);
273+
HashMap<String, String> externalUserIdToSupertokensUserIdMap = new HashMap<>();
274+
275+
List<String> supertokensOrExternalUserIdsToQuery = new ArrayList<>();
276+
for (String userId : supertokensUserIds) {
277+
if (supertokensUserIdToExternalUserIdMap.containsKey(userId)) {
278+
supertokensOrExternalUserIdsToQuery.add(supertokensUserIdToExternalUserIdMap.get(userId));
279+
externalUserIdToSupertokensUserIdMap.put(supertokensUserIdToExternalUserIdMap.get(userId), userId);
280+
} else {
281+
supertokensOrExternalUserIdsToQuery.add(userId);
282+
externalUserIdToSupertokensUserIdMap.put(userId, userId);
283+
}
284+
}
285+
286+
Map<String, String> supertokensOrExternalUserIdToEmailMap = new HashMap<>();
268287
for (UserIdAndEmail ue : userIdAndEmail) {
269-
if (userIdToEmailMap.containsKey(ue.userId)) {
288+
String supertokensOrExternalUserId = ue.userId;
289+
if (supertokensUserIdToExternalUserIdMap.containsKey(supertokensOrExternalUserId)) {
290+
supertokensOrExternalUserId = supertokensUserIdToExternalUserIdMap.get(supertokensOrExternalUserId);
291+
}
292+
if (supertokensOrExternalUserIdToEmailMap.containsKey(supertokensOrExternalUserId)) {
270293
throw new RuntimeException("Found a bug!");
271294
}
272-
userIdToEmailMap.put(ue.userId, ue.email);
295+
supertokensOrExternalUserIdToEmailMap.put(supertokensOrExternalUserId, ue.email);
273296
}
297+
274298
String QUERY = "SELECT * FROM " + getConfig(start).getEmailVerificationTable()
275-
+ " WHERE app_id = ? AND user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(userIds.size()) +
299+
+ " WHERE app_id = ? AND user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(supertokensOrExternalUserIdsToQuery.size()) +
276300
") AND email IN (" + Utils.generateCommaSeperatedQuestionMarks(emails.size()) + ")";
277301

278302
return execute(sqlCon, QUERY, pst -> {
279303
pst.setString(1, appIdentifier.getAppId());
280304
int index = 2;
281-
for (String userId : userIds) {
305+
for (String userId : supertokensOrExternalUserIdsToQuery) {
282306
pst.setString(index++, userId);
283307
}
284308
for (String email : emails) {
@@ -287,10 +311,10 @@ public static List<String> isEmailVerified_transaction(Start start, Connection s
287311
}, result -> {
288312
List<String> res = new ArrayList<>();
289313
while (result.next()) {
290-
String userId = result.getString("user_id");
314+
String supertokensOrExternalUserId = result.getString("user_id");
291315
String email = result.getString("email");
292-
if (Objects.equals(userIdToEmailMap.get(userId), email)) {
293-
res.add(userId);
316+
if (Objects.equals(supertokensOrExternalUserIdToEmailMap.get(supertokensOrExternalUserId), email)) {
317+
res.add(externalUserIdToSupertokensUserIdMap.get(supertokensOrExternalUserId));
294318
}
295319
}
296320
return res;
@@ -300,30 +324,51 @@ public static List<String> isEmailVerified_transaction(Start start, Connection s
300324
public static List<String> isEmailVerified(Start start, AppIdentifier appIdentifier,
301325
List<UserIdAndEmail> userIdAndEmail)
302326
throws SQLException, StorageQueryException {
327+
303328
if (userIdAndEmail.isEmpty()) {
304329
return new ArrayList<>();
305330
}
306331
List<String> emails = new ArrayList<>();
307-
List<String> userIds = new ArrayList<>();
308-
Map<String, String> userIdToEmailMap = new HashMap<>();
332+
List<String> supertokensUserIds = new ArrayList<>();
333+
309334
for (UserIdAndEmail ue : userIdAndEmail) {
310335
emails.add(ue.email);
311-
userIds.add(ue.userId);
336+
supertokensUserIds.add(ue.userId);
337+
}
338+
// We have external user id stored in the email verification table, so we need to fetch the mapped userids for
339+
// calculating the verified emails
340+
HashMap<String, String> supertokensUserIdToExternalUserIdMap = UserIdMappingQueries.getUserIdMappingWithUserIds(start,
341+
supertokensUserIds);
342+
HashMap<String, String> externalUserIdToSupertokensUserIdMap = new HashMap<>();
343+
List<String> supertokensOrExternalUserIdsToQuery = new ArrayList<>();
344+
for (String userId : supertokensUserIds) {
345+
if (supertokensUserIdToExternalUserIdMap.containsKey(userId)) {
346+
supertokensOrExternalUserIdsToQuery.add(supertokensUserIdToExternalUserIdMap.get(userId));
347+
externalUserIdToSupertokensUserIdMap.put(supertokensUserIdToExternalUserIdMap.get(userId), userId);
348+
} else {
349+
supertokensOrExternalUserIdsToQuery.add(userId);
350+
externalUserIdToSupertokensUserIdMap.put(userId, userId);
351+
}
312352
}
353+
354+
Map<String, String> supertokensOrExternalUserIdToEmailMap = new HashMap<>();
313355
for (UserIdAndEmail ue : userIdAndEmail) {
314-
if (userIdToEmailMap.containsKey(ue.userId)) {
356+
String supertokensOrExternalUserId = ue.userId;
357+
if (supertokensUserIdToExternalUserIdMap.containsKey(supertokensOrExternalUserId)) {
358+
supertokensOrExternalUserId = supertokensUserIdToExternalUserIdMap.get(supertokensOrExternalUserId);
359+
}
360+
if (supertokensOrExternalUserIdToEmailMap.containsKey(supertokensOrExternalUserId)) {
315361
throw new RuntimeException("Found a bug!");
316362
}
317-
userIdToEmailMap.put(ue.userId, ue.email);
363+
supertokensOrExternalUserIdToEmailMap.put(supertokensOrExternalUserId, ue.email);
318364
}
319365
String QUERY = "SELECT * FROM " + getConfig(start).getEmailVerificationTable()
320-
+ " WHERE app_id = ? AND user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(userIds.size()) +
366+
+ " WHERE app_id = ? AND user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(supertokensOrExternalUserIdsToQuery.size()) +
321367
") AND email IN (" + Utils.generateCommaSeperatedQuestionMarks(emails.size()) + ")";
322-
323368
return execute(start, QUERY, pst -> {
324369
pst.setString(1, appIdentifier.getAppId());
325370
int index = 2;
326-
for (String userId : userIds) {
371+
for (String userId : supertokensOrExternalUserIdsToQuery) {
327372
pst.setString(index++, userId);
328373
}
329374
for (String email : emails) {
@@ -332,10 +377,10 @@ public static List<String> isEmailVerified(Start start, AppIdentifier appIdentif
332377
}, result -> {
333378
List<String> res = new ArrayList<>();
334379
while (result.next()) {
335-
String userId = result.getString("user_id");
380+
String supertokensOrExternalUserId = result.getString("user_id");
336381
String email = result.getString("email");
337-
if (Objects.equals(userIdToEmailMap.get(userId), email)) {
338-
res.add(userId);
382+
if (Objects.equals(supertokensOrExternalUserIdToEmailMap.get(supertokensOrExternalUserId), email)) {
383+
res.add(externalUserIdToSupertokensUserIdMap.get(supertokensOrExternalUserId));
339384
}
340385
}
341386
return res;

src/main/java/io/supertokens/storage/postgresql/queries/UserIdMappingQueries.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.sql.SQLException;
3131
import java.util.ArrayList;
3232
import java.util.HashMap;
33+
import java.util.List;
3334

3435
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.execute;
3536
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.update;
@@ -127,7 +128,7 @@ public static UserIdMapping[] getUserIdMappingWithEitherSuperTokensUserIdOrExter
127128

128129
}
129130

130-
public static HashMap<String, String> getUserIdMappingWithUserIds(Start start, ArrayList<String> userIds)
131+
public static HashMap<String, String> getUserIdMappingWithUserIds(Start start, List<String> userIds)
131132
throws SQLException, StorageQueryException {
132133

133134
if (userIds.size() == 0) {
@@ -160,6 +161,39 @@ public static HashMap<String, String> getUserIdMappingWithUserIds(Start start, A
160161
});
161162
}
162163

164+
public static HashMap<String, String> getUserIdMappingWithUserIds_Transaction(Start start, Connection sqlCon, List<String> userIds)
165+
throws SQLException, StorageQueryException {
166+
167+
if (userIds.size() == 0) {
168+
return new HashMap<>();
169+
}
170+
171+
// No need to filter based on tenantId because the id list is already filtered for a tenant
172+
StringBuilder QUERY = new StringBuilder(
173+
"SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable() + " WHERE supertokens_user_id IN (");
174+
for (int i = 0; i < userIds.size(); i++) {
175+
QUERY.append("?");
176+
if (i != userIds.size() - 1) {
177+
// not the last element
178+
QUERY.append(",");
179+
}
180+
}
181+
QUERY.append(")");
182+
return execute(sqlCon, QUERY.toString(), pst -> {
183+
for (int i = 0; i < userIds.size(); i++) {
184+
// i+1 cause this starts with 1 and not 0
185+
pst.setString(i + 1, userIds.get(i));
186+
}
187+
}, result -> {
188+
HashMap<String, String> userIdMappings = new HashMap<>();
189+
while (result.next()) {
190+
UserIdMapping temp = UserIdMappingRowMapper.getInstance().mapOrThrow(result);
191+
userIdMappings.put(temp.superTokensUserId, temp.externalUserId);
192+
}
193+
return userIdMappings;
194+
});
195+
}
196+
163197
public static boolean deleteUserIdMappingWithSuperTokensUserId(Start start, AppIdentifier appIdentifier, String userId)
164198
throws SQLException, StorageQueryException {
165199
String QUERY = "DELETE FROM " + Config.getConfig(start).getUserIdMappingTable()

0 commit comments

Comments
 (0)