Skip to content

Commit 5ba88ce

Browse files
committed
fix: email verified in login methods
1 parent 45f662c commit 5ba88ce

File tree

2 files changed

+91
-10
lines changed

2 files changed

+91
-10
lines changed

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

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,20 +265,44 @@ public static List<String> isEmailVerified_transaction(Start start, Connection s
265265
emails.add(ue.email);
266266
userIds.add(ue.userId);
267267
}
268+
269+
// We have external user id stored in the email verification table, so we need to fetch the mapped userids for
270+
// calculating the verified emails
271+
272+
HashMap<String, String> userIdMappings = UserIdMappingQueries.getUserIdMappingWithUserIds_Transaction(start,
273+
sqlCon, userIds);
274+
HashMap<String, String> externalUserIdMappings = new HashMap<>();
275+
276+
List<String> userIdsToQuery = new ArrayList<>();
277+
for (String userId : userIds) {
278+
if (userIdMappings.containsKey(userId)) {
279+
userIdsToQuery.add(userIdMappings.get(userId));
280+
externalUserIdMappings.put(userIdMappings.get(userId), userId);
281+
} else {
282+
userIdsToQuery.add(userId);
283+
externalUserIdMappings.put(userId, userId);
284+
}
285+
}
286+
268287
for (UserIdAndEmail ue : userIdAndEmail) {
269-
if (userIdToEmailMap.containsKey(ue.userId)) {
288+
String userId = ue.userId;
289+
if (userIdMappings.containsKey(userId)) {
290+
userId = userIdMappings.get(userId);
291+
}
292+
if (userIdToEmailMap.containsKey(userId)) {
270293
throw new RuntimeException("Found a bug!");
271294
}
272-
userIdToEmailMap.put(ue.userId, ue.email);
295+
userIdToEmailMap.put(userId, 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(userIdsToQuery.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 : userIdsToQuery) {
282306
pst.setString(index++, userId);
283307
}
284308
for (String email : emails) {
@@ -290,7 +314,7 @@ public static List<String> isEmailVerified_transaction(Start start, Connection s
290314
String userId = result.getString("user_id");
291315
String email = result.getString("email");
292316
if (Objects.equals(userIdToEmailMap.get(userId), email)) {
293-
res.add(userId);
317+
res.add(externalUserIdMappings.get(userId));
294318
}
295319
}
296320
return res;
@@ -310,11 +334,34 @@ public static List<String> isEmailVerified(Start start, AppIdentifier appIdentif
310334
emails.add(ue.email);
311335
userIds.add(ue.userId);
312336
}
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+
341+
HashMap<String, String> userIdMappings = UserIdMappingQueries.getUserIdMappingWithUserIds(start,
342+
userIds);
343+
HashMap<String, String> externalUserIdMappings = new HashMap<>();
344+
345+
List<String> userIdsToQuery = new ArrayList<>();
346+
for (String userId : userIds) {
347+
if (userIdMappings.containsKey(userId)) {
348+
userIdsToQuery.add(userIdMappings.get(userId));
349+
externalUserIdMappings.put(userIdMappings.get(userId), userId);
350+
} else {
351+
userIdsToQuery.add(userId);
352+
externalUserIdMappings.put(userId, userId);
353+
}
354+
}
355+
313356
for (UserIdAndEmail ue : userIdAndEmail) {
314-
if (userIdToEmailMap.containsKey(ue.userId)) {
357+
String userId = ue.userId;
358+
if (userIdMappings.containsKey(userId)) {
359+
userId = userIdMappings.get(userId);
360+
}
361+
if (userIdToEmailMap.containsKey(userId)) {
315362
throw new RuntimeException("Found a bug!");
316363
}
317-
userIdToEmailMap.put(ue.userId, ue.email);
364+
userIdToEmailMap.put(userId, ue.email);
318365
}
319366
String QUERY = "SELECT * FROM " + getConfig(start).getEmailVerificationTable()
320367
+ " WHERE app_id = ? AND user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(userIds.size()) +
@@ -323,7 +370,7 @@ public static List<String> isEmailVerified(Start start, AppIdentifier appIdentif
323370
return execute(start, QUERY, pst -> {
324371
pst.setString(1, appIdentifier.getAppId());
325372
int index = 2;
326-
for (String userId : userIds) {
373+
for (String userId : userIdsToQuery) {
327374
pst.setString(index++, userId);
328375
}
329376
for (String email : emails) {
@@ -335,7 +382,7 @@ public static List<String> isEmailVerified(Start start, AppIdentifier appIdentif
335382
String userId = result.getString("user_id");
336383
String email = result.getString("email");
337384
if (Objects.equals(userIdToEmailMap.get(userId), email)) {
338-
res.add(userId);
385+
res.add(externalUserIdMappings.get(userId));
339386
}
340387
}
341388
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)