Skip to content

Commit b42a470

Browse files
authored
fix: email verification with user id mapping (#172)
1 parent e5c2e00 commit b42a470

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
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.3] - 2023-11-10
11+
12+
- Fixes issue with email verification with user id mapping
13+
1014
## [5.0.2] - 2023-11-01
1115

1216
- Fixes `verified` in `loginMethods` for users with userId mapping

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.2"
5+
version = "5.0.3"
66

77
repositories {
88
mavenCentral()

src/main/java/io/supertokens/storage/postgresql/Start.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,11 @@ public boolean isEmailVerified(AppIdentifier appIdentifier, String userId, Strin
11821182
}
11831183
}
11841184

1185+
@Override
1186+
public void updateIsEmailVerifiedToExternalUserId(AppIdentifier appIdentifier, String supertokensUserId, String externalUserId) throws StorageQueryException {
1187+
EmailVerificationQueries.updateIsEmailVerifiedToExternalUserId(this, appIdentifier, supertokensUserId, externalUserId);
1188+
}
1189+
11851190
@Override
11861191
public void deleteExpiredPasswordResetTokens() throws StorageQueryException {
11871192
try {

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

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import io.supertokens.pluginInterface.RowMapper;
2020
import io.supertokens.pluginInterface.emailverification.EmailVerificationTokenInfo;
2121
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
22+
import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException;
2223
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
2324
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
25+
import io.supertokens.pluginInterface.sqlStorage.TransactionConnection;
2426
import io.supertokens.storage.postgresql.Start;
2527
import io.supertokens.storage.postgresql.config.Config;
2628
import io.supertokens.storage.postgresql.utils.Utils;
@@ -450,13 +452,63 @@ public static void revokeAllTokens(Start start, TenantIdentifier tenantIdentifie
450452

451453
public static boolean isUserIdBeingUsedForEmailVerification(Start start, AppIdentifier appIdentifier, String userId)
452454
throws SQLException, StorageQueryException {
453-
String QUERY = "SELECT * FROM " + getConfig(start).getEmailVerificationTokensTable()
454-
+ " WHERE app_id = ? AND user_id = ?";
455+
{
456+
String QUERY = "SELECT * FROM " + getConfig(start).getEmailVerificationTokensTable()
457+
+ " WHERE app_id = ? AND user_id = ?";
455458

456-
return execute(start, QUERY, pst -> {
457-
pst.setString(1, appIdentifier.getAppId());
458-
pst.setString(2, userId);
459-
}, ResultSet::next);
459+
boolean isUsed = execute(start, QUERY, pst -> {
460+
pst.setString(1, appIdentifier.getAppId());
461+
pst.setString(2, userId);
462+
}, ResultSet::next);
463+
if (isUsed) {
464+
return true;
465+
}
466+
}
467+
468+
{
469+
String QUERY = "SELECT * FROM " + getConfig(start).getEmailVerificationTable()
470+
+ " WHERE app_id = ? AND user_id = ?";
471+
472+
return execute(start, QUERY, pst -> {
473+
pst.setString(1, appIdentifier.getAppId());
474+
pst.setString(2, userId);
475+
}, ResultSet::next);
476+
}
477+
}
478+
479+
public static void updateIsEmailVerifiedToExternalUserId(Start start, AppIdentifier appIdentifier, String supertokensUserId, String externalUserId)
480+
throws StorageQueryException {
481+
try {
482+
start.startTransaction((TransactionConnection con) -> {
483+
Connection sqlCon = (Connection) con.getConnection();
484+
try {
485+
{
486+
String QUERY = "UPDATE " + getConfig(start).getEmailVerificationTable()
487+
+ " SET user_id = ? WHERE app_id = ? AND user_id = ?";
488+
update(sqlCon, QUERY, pst -> {
489+
pst.setString(1, externalUserId);
490+
pst.setString(2, appIdentifier.getAppId());
491+
pst.setString(3, supertokensUserId);
492+
});
493+
}
494+
{
495+
String QUERY = "UPDATE " + getConfig(start).getEmailVerificationTokensTable()
496+
+ " SET user_id = ? WHERE app_id = ? AND user_id = ?";
497+
update(sqlCon, QUERY, pst -> {
498+
pst.setString(1, externalUserId);
499+
pst.setString(2, appIdentifier.getAppId());
500+
pst.setString(3, supertokensUserId);
501+
});
502+
}
503+
} catch (SQLException e) {
504+
throw new StorageTransactionLogicException(e);
505+
}
506+
507+
return null;
508+
});
509+
} catch (StorageTransactionLogicException e) {
510+
throw new StorageQueryException(e.actualException);
511+
}
460512
}
461513

462514
private static class EmailVerificationTokenInfoRowMapper

0 commit comments

Comments
 (0)