Skip to content

Commit

Permalink
fix: email verification with user id mapping (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc authored Nov 10, 2023
1 parent 9a0141a commit bc121d4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id 'java-library'
}

version = "5.0.2"
version = "5.0.3"

repositories {
mavenCentral()
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/supertokens/storage/mysql/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bc121d4

Please sign in to comment.