Skip to content

Commit

Permalink
fix: email verified in login methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Nov 1, 2023
1 parent 45f662c commit 5ba88ce
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,20 +265,44 @@ public static List<String> isEmailVerified_transaction(Start start, Connection s
emails.add(ue.email);
userIds.add(ue.userId);
}

// We have external user id stored in the email verification table, so we need to fetch the mapped userids for
// calculating the verified emails

HashMap<String, String> userIdMappings = UserIdMappingQueries.getUserIdMappingWithUserIds_Transaction(start,
sqlCon, userIds);
HashMap<String, String> externalUserIdMappings = new HashMap<>();

List<String> userIdsToQuery = new ArrayList<>();
for (String userId : userIds) {
if (userIdMappings.containsKey(userId)) {
userIdsToQuery.add(userIdMappings.get(userId));
externalUserIdMappings.put(userIdMappings.get(userId), userId);
} else {
userIdsToQuery.add(userId);
externalUserIdMappings.put(userId, userId);
}
}

for (UserIdAndEmail ue : userIdAndEmail) {
if (userIdToEmailMap.containsKey(ue.userId)) {
String userId = ue.userId;
if (userIdMappings.containsKey(userId)) {
userId = userIdMappings.get(userId);
}
if (userIdToEmailMap.containsKey(userId)) {
throw new RuntimeException("Found a bug!");
}
userIdToEmailMap.put(ue.userId, ue.email);
userIdToEmailMap.put(userId, ue.email);
}

String QUERY = "SELECT * FROM " + getConfig(start).getEmailVerificationTable()
+ " WHERE app_id = ? AND user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(userIds.size()) +
+ " WHERE app_id = ? AND user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(userIdsToQuery.size()) +
") AND email IN (" + Utils.generateCommaSeperatedQuestionMarks(emails.size()) + ")";

return execute(sqlCon, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
int index = 2;
for (String userId : userIds) {
for (String userId : userIdsToQuery) {
pst.setString(index++, userId);
}
for (String email : emails) {
Expand All @@ -290,7 +314,7 @@ public static List<String> isEmailVerified_transaction(Start start, Connection s
String userId = result.getString("user_id");
String email = result.getString("email");
if (Objects.equals(userIdToEmailMap.get(userId), email)) {
res.add(userId);
res.add(externalUserIdMappings.get(userId));
}
}
return res;
Expand All @@ -310,11 +334,34 @@ public static List<String> isEmailVerified(Start start, AppIdentifier appIdentif
emails.add(ue.email);
userIds.add(ue.userId);
}

// We have external user id stored in the email verification table, so we need to fetch the mapped userids for
// calculating the verified emails

HashMap<String, String> userIdMappings = UserIdMappingQueries.getUserIdMappingWithUserIds(start,
userIds);
HashMap<String, String> externalUserIdMappings = new HashMap<>();

List<String> userIdsToQuery = new ArrayList<>();
for (String userId : userIds) {
if (userIdMappings.containsKey(userId)) {
userIdsToQuery.add(userIdMappings.get(userId));
externalUserIdMappings.put(userIdMappings.get(userId), userId);
} else {
userIdsToQuery.add(userId);
externalUserIdMappings.put(userId, userId);
}
}

for (UserIdAndEmail ue : userIdAndEmail) {
if (userIdToEmailMap.containsKey(ue.userId)) {
String userId = ue.userId;
if (userIdMappings.containsKey(userId)) {
userId = userIdMappings.get(userId);
}
if (userIdToEmailMap.containsKey(userId)) {
throw new RuntimeException("Found a bug!");
}
userIdToEmailMap.put(ue.userId, ue.email);
userIdToEmailMap.put(userId, ue.email);
}
String QUERY = "SELECT * FROM " + getConfig(start).getEmailVerificationTable()
+ " WHERE app_id = ? AND user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(userIds.size()) +
Expand All @@ -323,7 +370,7 @@ public static List<String> isEmailVerified(Start start, AppIdentifier appIdentif
return execute(start, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
int index = 2;
for (String userId : userIds) {
for (String userId : userIdsToQuery) {
pst.setString(index++, userId);
}
for (String email : emails) {
Expand All @@ -335,7 +382,7 @@ public static List<String> isEmailVerified(Start start, AppIdentifier appIdentif
String userId = result.getString("user_id");
String email = result.getString("email");
if (Objects.equals(userIdToEmailMap.get(userId), email)) {
res.add(userId);
res.add(externalUserIdMappings.get(userId));
}
}
return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static io.supertokens.storage.postgresql.QueryExecutorTemplate.execute;
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.update;
Expand Down Expand Up @@ -127,7 +128,7 @@ public static UserIdMapping[] getUserIdMappingWithEitherSuperTokensUserIdOrExter

}

public static HashMap<String, String> getUserIdMappingWithUserIds(Start start, ArrayList<String> userIds)
public static HashMap<String, String> getUserIdMappingWithUserIds(Start start, List<String> userIds)
throws SQLException, StorageQueryException {

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

public static HashMap<String, String> getUserIdMappingWithUserIds_Transaction(Start start, Connection sqlCon, List<String> userIds)
throws SQLException, StorageQueryException {

if (userIds.size() == 0) {
return new HashMap<>();
}

// No need to filter based on tenantId because the id list is already filtered for a tenant
StringBuilder QUERY = new StringBuilder(
"SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable() + " WHERE supertokens_user_id IN (");
for (int i = 0; i < userIds.size(); i++) {
QUERY.append("?");
if (i != userIds.size() - 1) {
// not the last element
QUERY.append(",");
}
}
QUERY.append(")");
return execute(sqlCon, QUERY.toString(), pst -> {
for (int i = 0; i < userIds.size(); i++) {
// i+1 cause this starts with 1 and not 0
pst.setString(i + 1, userIds.get(i));
}
}, result -> {
HashMap<String, String> userIdMappings = new HashMap<>();
while (result.next()) {
UserIdMapping temp = UserIdMappingRowMapper.getInstance().mapOrThrow(result);
userIdMappings.put(temp.superTokensUserId, temp.externalUserId);
}
return userIdMappings;
});
}

public static boolean deleteUserIdMappingWithSuperTokensUserId(Start start, AppIdentifier appIdentifier, String userId)
throws SQLException, StorageQueryException {
String QUERY = "DELETE FROM " + Config.getConfig(start).getUserIdMappingTable()
Expand Down

0 comments on commit 5ba88ce

Please sign in to comment.