From 3c54cf96d476f2a876121cc7193ca795bf79f235 Mon Sep 17 00:00:00 2001 From: Sattvik Chakravarthy Date: Fri, 10 Nov 2023 12:24:20 +0530 Subject: [PATCH] fix: email verification with user id mapping --- CHANGELOG.md | 4 ++ build.gradle | 2 +- .../io/supertokens/storage/mysql/Start.java | 5 ++ .../queries/EmailVerificationQueries.java | 63 +++++++++++++++++-- 4 files changed, 67 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c6e681..802c104 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +## [5.0.3] - 2023-11-10 + +- Fixes issue with email verification with user id mapping + ## [5.0.2] - 2023-11-01 - Fixes `verified` in `loginMethods` for users with userId mapping diff --git a/build.gradle b/build.gradle index 5b5aeb2..92cc3ea 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,7 @@ plugins { id 'java-library' } -version = "5.0.2" +version = "5.0.3" repositories { mavenCentral() diff --git a/src/main/java/io/supertokens/storage/mysql/Start.java b/src/main/java/io/supertokens/storage/mysql/Start.java index 6f3385b..2222dc2 100644 --- a/src/main/java/io/supertokens/storage/mysql/Start.java +++ b/src/main/java/io/supertokens/storage/mysql/Start.java @@ -1144,6 +1144,11 @@ public boolean isEmailVerified(AppIdentifier appIdentifier, String userId, Strin } } + @Override + public void updateIsEmailVerifiedToExternalUserId(AppIdentifier appIdentifier, String supertokensUserId, String externalUserId) throws StorageQueryException { + EmailVerificationQueries.updateIsEmailVerifiedToExternalUserId(this, appIdentifier, supertokensUserId, externalUserId); + } + @Override public void deleteExpiredPasswordResetTokens() throws StorageQueryException { try { diff --git a/src/main/java/io/supertokens/storage/mysql/queries/EmailVerificationQueries.java b/src/main/java/io/supertokens/storage/mysql/queries/EmailVerificationQueries.java index e66d014..eee5a00 100644 --- a/src/main/java/io/supertokens/storage/mysql/queries/EmailVerificationQueries.java +++ b/src/main/java/io/supertokens/storage/mysql/queries/EmailVerificationQueries.java @@ -22,6 +22,7 @@ import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException; import io.supertokens.pluginInterface.multitenancy.AppIdentifier; import io.supertokens.pluginInterface.multitenancy.TenantIdentifier; +import io.supertokens.pluginInterface.sqlStorage.TransactionConnection; import io.supertokens.storage.mysql.Start; import io.supertokens.storage.mysql.config.Config; import io.supertokens.storage.mysql.utils.Utils; @@ -220,6 +221,41 @@ public static boolean isEmailVerified(Start start, AppIdentifier appIdentifier, }, result -> result.next()); } + public static void updateIsEmailVerifiedToExternalUserId(Start start, AppIdentifier appIdentifier, String supertokensUserId, String externalUserId) + throws StorageQueryException { + try { + start.startTransaction((TransactionConnection con) -> { + Connection sqlCon = (Connection) con.getConnection(); + try { + { + String QUERY = "UPDATE " + getConfig(start).getEmailVerificationTable() + + " SET user_id = ? WHERE app_id = ? AND user_id = ?"; + update(sqlCon, QUERY, pst -> { + pst.setString(1, externalUserId); + pst.setString(2, appIdentifier.getAppId()); + pst.setString(3, supertokensUserId); + }); + } + { + String QUERY = "UPDATE " + getConfig(start).getEmailVerificationTokensTable() + + " SET user_id = ? WHERE app_id = ? AND user_id = ?"; + update(sqlCon, QUERY, pst -> { + pst.setString(1, externalUserId); + pst.setString(2, appIdentifier.getAppId()); + pst.setString(3, supertokensUserId); + }); + } + } catch (SQLException e) { + throw new StorageTransactionLogicException(e); + } + + return null; + }); + } catch (StorageTransactionLogicException e) { + throw new StorageQueryException(e.actualException); + } + } + public static class UserIdAndEmail { public String userId; public String email; @@ -428,13 +464,28 @@ public static void revokeAllTokens(Start start, TenantIdentifier tenantIdentifie public static boolean isUserIdBeingUsedForEmailVerification(Start start, AppIdentifier appIdentifier, String userId) throws SQLException, StorageQueryException { - String QUERY = "SELECT * FROM " + getConfig(start).getEmailVerificationTokensTable() - + " WHERE app_id = ? AND user_id = ?"; + { + String QUERY = "SELECT * FROM " + getConfig(start).getEmailVerificationTokensTable() + + " WHERE app_id = ? AND user_id = ?"; - return execute(start, QUERY, pst -> { - pst.setString(1, appIdentifier.getAppId()); - pst.setString(2, userId); - }, ResultSet::next); + boolean isUsed = execute(start, QUERY, pst -> { + pst.setString(1, appIdentifier.getAppId()); + pst.setString(2, userId); + }, ResultSet::next); + if (isUsed) { + return true; + } + } + + { + String QUERY = "SELECT * FROM " + getConfig(start).getEmailVerificationTable() + + " WHERE app_id = ? AND user_id = ?"; + + return execute(start, QUERY, pst -> { + pst.setString(1, appIdentifier.getAppId()); + pst.setString(2, userId); + }, ResultSet::next); + } } private static class EmailVerificationTokenInfoRowMapper