Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: email verification with user id mapping #83

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading