|
19 | 19 | import io.supertokens.pluginInterface.RowMapper;
|
20 | 20 | import io.supertokens.pluginInterface.emailverification.EmailVerificationTokenInfo;
|
21 | 21 | import io.supertokens.pluginInterface.exceptions.StorageQueryException;
|
| 22 | +import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException; |
22 | 23 | import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
|
23 | 24 | import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
|
| 25 | +import io.supertokens.pluginInterface.sqlStorage.TransactionConnection; |
24 | 26 | import io.supertokens.storage.postgresql.Start;
|
25 | 27 | import io.supertokens.storage.postgresql.config.Config;
|
26 | 28 | import io.supertokens.storage.postgresql.utils.Utils;
|
@@ -450,13 +452,63 @@ public static void revokeAllTokens(Start start, TenantIdentifier tenantIdentifie
|
450 | 452 |
|
451 | 453 | public static boolean isUserIdBeingUsedForEmailVerification(Start start, AppIdentifier appIdentifier, String userId)
|
452 | 454 | 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 = ?"; |
455 | 458 |
|
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 | + } |
460 | 512 | }
|
461 | 513 |
|
462 | 514 | private static class EmailVerificationTokenInfoRowMapper
|
|
0 commit comments