Skip to content

Commit

Permalink
fix: password reset changes (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc authored Sep 13, 2023
1 parent 8daf1d1 commit 36098ff
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/supertokens/inmemorydb/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ public void addPasswordResetToken(AppIdentifier appIdentifier, PasswordResetToke
throws StorageQueryException, UnknownUserIdException, DuplicatePasswordResetTokenException {
try {
EmailPasswordQueries.addPasswordResetToken(this, appIdentifier, passwordResetTokenInfo.userId,
passwordResetTokenInfo.token, passwordResetTokenInfo.tokenExpiry);
passwordResetTokenInfo.token, passwordResetTokenInfo.tokenExpiry, passwordResetTokenInfo.email);
} catch (SQLException e) {
if (e instanceof SQLiteException) {
String serverMessage = e.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static String getQueryToCreatePasswordResetTokensTable(Start start) {
+ "token VARCHAR(128) NOT NULL UNIQUE,"
+ "token_expiry BIGINT UNSIGNED NOT NULL,"
+ "PRIMARY KEY (app_id, user_id, token),"
+ "FOREIGN KEY (app_id, user_id) REFERENCES " + Config.getConfig(start).getEmailPasswordUsersTable()
+ "FOREIGN KEY (app_id, user_id) REFERENCES " + Config.getConfig(start).getAppIdToUserIdTable()
+ " (app_id, user_id) ON DELETE CASCADE ON UPDATE CASCADE"
+ ");";
}
Expand Down Expand Up @@ -215,17 +215,30 @@ public static PasswordResetTokenInfo getPasswordResetTokenInfo(Start start, AppI
}

public static void addPasswordResetToken(Start start, AppIdentifier appIdentifier, String userId, String tokenHash,
long expiry)
long expiry, String email)
throws SQLException, StorageQueryException {
String QUERY = "INSERT INTO " + getConfig(start).getPasswordResetTokensTable()
+ "(app_id, user_id, token, token_expiry)" + " VALUES(?, ?, ?, ?)";
if (email != null) {
String QUERY = "INSERT INTO " + getConfig(start).getPasswordResetTokensTable()
+ "(app_id, user_id, token, token_expiry, email)" + " VALUES(?, ?, ?, ?, ?)";

update(start, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, userId);
pst.setString(3, tokenHash);
pst.setLong(4, expiry);
});
update(start, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, userId);
pst.setString(3, tokenHash);
pst.setLong(4, expiry);
pst.setString(5, email);
});
} else {
String QUERY = "INSERT INTO " + getConfig(start).getPasswordResetTokensTable()
+ "(app_id, user_id, token, token_expiry)" + " VALUES(?, ?, ?, ?)";

update(start, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, userId);
pst.setString(3, tokenHash);
pst.setLong(4, expiry);
});
}
}

public static AuthRecipeUserInfo signUp(Start start, TenantIdentifier tenantIdentifier, String userId, String email,
Expand Down Expand Up @@ -322,6 +335,15 @@ public static void deleteUser_Transaction(Connection sqlCon, Start start, AppIde
pst.setString(2, userId);
});
}

{
String QUERY = "DELETE FROM " + getConfig(start).getPasswordResetTokensTable()
+ " WHERE app_id = ? AND user_id = ?";
update(sqlCon, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, userId);
});
}
}
}

Expand Down

0 comments on commit 36098ff

Please sign in to comment.