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: recipeUserId in sign in/up related APIs #769

Merged
merged 8 commits into from
Aug 24, 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
60 changes: 30 additions & 30 deletions src/main/java/io/supertokens/authRecipe/AuthRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public static boolean unlinkAccounts(Main main, AppIdentifierWithStorage appIden
appIdentifierWithStorage,
recipeUserId, UserIdType.SUPERTOKENS);

if (primaryUser.id.equals(recipeUserId)) {
if (primaryUser.getSupertokensUserId().equals(recipeUserId)) {
// we are trying to unlink the user ID which is the same as the primary one.
if (primaryUser.loginMethods.length == 1) {
storage.unlinkAccounts_Transaction(appIdentifierWithStorage, con, primaryUser.id, recipeUserId);
storage.unlinkAccounts_Transaction(appIdentifierWithStorage, con, primaryUser.getSupertokensUserId(), recipeUserId);
Session.revokeAllSessionsForUser(main, appIdentifierWithStorage,
mappingResult == null ? recipeUserId : mappingResult.externalUserId,
false);
Expand All @@ -109,7 +109,7 @@ public static boolean unlinkAccounts(Main main, AppIdentifierWithStorage appIden
return true;
}
} else {
storage.unlinkAccounts_Transaction(appIdentifierWithStorage, con, primaryUser.id, recipeUserId);
storage.unlinkAccounts_Transaction(appIdentifierWithStorage, con, primaryUser.getSupertokensUserId(), recipeUserId);
Session.revokeAllSessionsForUser(main, appIdentifierWithStorage,
mappingResult == null ? recipeUserId : mappingResult.externalUserId, false);
return false;
Expand Down Expand Up @@ -221,7 +221,7 @@ private static CanLinkAccountsResult canLinkAccountsHelper(TransactionConnection
}

if (!primaryUser.isPrimaryUser) {
throw new InputUserIdIsNotAPrimaryUserException(primaryUser.id);
throw new InputUserIdIsNotAPrimaryUserException(primaryUser.getSupertokensUserId());
}

AuthRecipeUserInfo recipeUser = storage.getPrimaryUserById_Transaction(appIdentifierWithStorage, con,
Expand All @@ -231,10 +231,10 @@ private static CanLinkAccountsResult canLinkAccountsHelper(TransactionConnection
}

if (recipeUser.isPrimaryUser) {
if (recipeUser.id.equals(primaryUser.id)) {
return new CanLinkAccountsResult(recipeUser.id, primaryUser.id, true);
if (recipeUser.getSupertokensUserId().equals(primaryUser.getSupertokensUserId())) {
return new CanLinkAccountsResult(recipeUser.getSupertokensUserId(), primaryUser.getSupertokensUserId(), true);
} else {
throw new RecipeUserIdAlreadyLinkedWithAnotherPrimaryUserIdException(recipeUser.id,
throw new RecipeUserIdAlreadyLinkedWithAnotherPrimaryUserIdException(recipeUser.getSupertokensUserId(),
"The input recipe user ID is already linked to another user ID");
}
}
Expand Down Expand Up @@ -270,8 +270,8 @@ private static CanLinkAccountsResult canLinkAccountsHelper(TransactionConnection
.listPrimaryUsersByEmail_Transaction(tenantIdentifier, con,
recipeUserIdLM.email);
for (AuthRecipeUserInfo user : usersWithSameEmail) {
if (user.isPrimaryUser && !user.id.equals(primaryUser.id)) {
throw new AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdException(user.id,
if (user.isPrimaryUser && !user.getSupertokensUserId().equals(primaryUser.getSupertokensUserId())) {
throw new AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdException(user.getSupertokensUserId(),
"This user's email is already associated with another user ID");
}
}
Expand All @@ -282,8 +282,8 @@ private static CanLinkAccountsResult canLinkAccountsHelper(TransactionConnection
.listPrimaryUsersByPhoneNumber_Transaction(tenantIdentifier, con,
recipeUserIdLM.phoneNumber);
for (AuthRecipeUserInfo user : usersWithSamePhoneNumber) {
if (user.isPrimaryUser && !user.id.equals(primaryUser.id)) {
throw new AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdException(user.id,
if (user.isPrimaryUser && !user.getSupertokensUserId().equals(primaryUser.getSupertokensUserId())) {
throw new AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdException(user.getSupertokensUserId(),
"This user's phone number is already associated with another user" +
" ID");
}
Expand All @@ -295,16 +295,16 @@ private static CanLinkAccountsResult canLinkAccountsHelper(TransactionConnection
.getPrimaryUsersByThirdPartyInfo_Transaction(tenantIdentifier, con,
recipeUserIdLM.thirdParty.id, recipeUserIdLM.thirdParty.userId);
if (userWithSameThirdParty != null && userWithSameThirdParty.isPrimaryUser &&
!userWithSameThirdParty.id.equals(primaryUser.id)) {
!userWithSameThirdParty.getSupertokensUserId().equals(primaryUser.getSupertokensUserId())) {
throw new AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdException(
userWithSameThirdParty.id,
userWithSameThirdParty.getSupertokensUserId(),
"This user's third party login is already associated with another" +
" user ID");
}
}
}

return new CanLinkAccountsResult(recipeUser.id, primaryUser.id, false);
return new CanLinkAccountsResult(recipeUser.getSupertokensUserId(), primaryUser.getSupertokensUserId(), false);
}

@TestOnly
Expand Down Expand Up @@ -437,10 +437,10 @@ private static CreatePrimaryUserResult canCreatePrimaryUserHelper(TransactionCon
throw new UnknownUserIdException();
}
if (targetUser.isPrimaryUser) {
if (targetUser.id.equals(recipeUserId)) {
if (targetUser.getSupertokensUserId().equals(recipeUserId)) {
return new CreatePrimaryUserResult(targetUser, true);
} else {
throw new RecipeUserIdAlreadyLinkedWithPrimaryUserIdException(targetUser.id,
throw new RecipeUserIdAlreadyLinkedWithPrimaryUserIdException(targetUser.getSupertokensUserId(),
"This user ID is already linked to another user ID");
}
}
Expand All @@ -464,7 +464,7 @@ private static CreatePrimaryUserResult canCreatePrimaryUserHelper(TransactionCon
loginMethod.email);
for (AuthRecipeUserInfo user : usersWithSameEmail) {
if (user.isPrimaryUser) {
throw new AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdException(user.id,
throw new AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdException(user.getSupertokensUserId(),
"This user's email is already associated with another user ID");
}
}
Expand All @@ -476,7 +476,7 @@ private static CreatePrimaryUserResult canCreatePrimaryUserHelper(TransactionCon
loginMethod.phoneNumber);
for (AuthRecipeUserInfo user : usersWithSamePhoneNumber) {
if (user.isPrimaryUser) {
throw new AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdException(user.id,
throw new AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdException(user.getSupertokensUserId(),
"This user's phone number is already associated with another user" +
" ID");
}
Expand All @@ -489,7 +489,7 @@ private static CreatePrimaryUserResult canCreatePrimaryUserHelper(TransactionCon
loginMethod.thirdParty.id, loginMethod.thirdParty.userId);
if (userWithSameThirdParty != null && userWithSameThirdParty.isPrimaryUser) {
throw new AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdException(
userWithSameThirdParty.id,
userWithSameThirdParty.getSupertokensUserId(),
"This user's third party login is already associated with another" +
" user ID");
}
Expand Down Expand Up @@ -537,7 +537,7 @@ public static CreatePrimaryUserResult createPrimaryUser(Main main,
if (result.wasAlreadyAPrimaryUser) {
return result;
}
storage.makePrimaryUser_Transaction(appIdentifierWithStorage, con, result.user.id);
storage.makePrimaryUser_Transaction(appIdentifierWithStorage, con, result.user.getSupertokensUserId());

storage.commitTransaction(con);

Expand Down Expand Up @@ -681,7 +681,7 @@ public static UserPaginationContainer getUsers(TenantIdentifierWithStorage tenan
int maxLoop = users.length;
if (users.length == limit + 1) {
maxLoop = limit;
nextPaginationToken = new UserPaginationToken(users[limit].id,
nextPaginationToken = new UserPaginationToken(users[limit].getSupertokensUserId(),
users[limit].timeJoined).generateToken();
}
AuthRecipeUserInfo[] resultUsers = new AuthRecipeUserInfo[maxLoop];
Expand Down Expand Up @@ -791,23 +791,23 @@ private static void deleteUserHelper(TransactionConnection con, AppIdentifierWit
}

if (removeAllLinkedAccounts || userToDelete.loginMethods.length == 1) {
if (userToDelete.id.equals(userIdToDeleteForAuthRecipe)) {
if (userToDelete.getSupertokensUserId().equals(userIdToDeleteForAuthRecipe)) {
primaryUserIdToDeleteNonAuthRecipe = userIdToDeleteForNonAuthRecipeForRecipeUserId;
} else {
// this is always type supertokens user ID cause it's from a user from the database.
io.supertokens.pluginInterface.useridmapping.UserIdMapping mappingResult =
io.supertokens.useridmapping.UserIdMapping.getUserIdMapping(
appIdentifierWithStorage,
userToDelete.id, UserIdType.SUPERTOKENS);
userToDelete.getSupertokensUserId(), UserIdType.SUPERTOKENS);
if (mappingResult != null) {
primaryUserIdToDeleteNonAuthRecipe = mappingResult.externalUserId;
} else {
primaryUserIdToDeleteNonAuthRecipe = userToDelete.id;
primaryUserIdToDeleteNonAuthRecipe = userToDelete.getSupertokensUserId();
}

}
} else {
if (userToDelete.id.equals(userIdToDeleteForAuthRecipe)) {
if (userToDelete.getSupertokensUserId().equals(userIdToDeleteForAuthRecipe)) {
// this means we are deleting the primary user itself, but keeping other linked accounts
// so we keep the non auth recipe info of this user since other linked accounts can use it
userIdToDeleteForNonAuthRecipeForRecipeUserId = null;
Expand All @@ -816,7 +816,7 @@ private static void deleteUserHelper(TransactionConnection con, AppIdentifierWit

if (!removeAllLinkedAccounts) {
deleteAuthRecipeUser(con, appIdentifierWithStorage, userIdToDeleteForAuthRecipe,
!userIdToDeleteForAuthRecipe.equals(userToDelete.id));
!userIdToDeleteForAuthRecipe.equals(userToDelete.getSupertokensUserId()));

if (userIdToDeleteForNonAuthRecipeForRecipeUserId != null) {
deleteNonAuthRecipeUser(con, appIdentifierWithStorage, userIdToDeleteForNonAuthRecipeForRecipeUserId);
Expand All @@ -827,17 +827,17 @@ private static void deleteUserHelper(TransactionConnection con, AppIdentifierWit

// this is only done to also delete the user ID mapping in case it exists, since we do not delete in the
// previous call to deleteAuthRecipeUser above.
deleteAuthRecipeUser(con, appIdentifierWithStorage, userToDelete.id,
deleteAuthRecipeUser(con, appIdentifierWithStorage, userToDelete.getSupertokensUserId(),
true);
}
} else {
for (LoginMethod lM : userToDelete.loginMethods) {
io.supertokens.pluginInterface.useridmapping.UserIdMapping mappingResult = lM.recipeUserId.equals(
io.supertokens.pluginInterface.useridmapping.UserIdMapping mappingResult = lM.getSupertokensUserId().equals(
userIdToDeleteForAuthRecipe) ? userIdMapping :
io.supertokens.useridmapping.UserIdMapping.getUserIdMapping(
appIdentifierWithStorage,
lM.recipeUserId, UserIdType.SUPERTOKENS);
deleteUserHelper(con, appIdentifierWithStorage, lM.recipeUserId, false, mappingResult);
lM.getSupertokensUserId(), UserIdType.SUPERTOKENS);
deleteUserHelper(con, appIdentifierWithStorage, lM.getSupertokensUserId(), false, mappingResult);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/supertokens/emailpassword/EmailPassword.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public static ImportUserResponse importUserWithPasswordHash(TenantIdentifierWith
LoginMethod finalLoginMethod = loginMethod;
storage.startTransaction(con -> {
storage.updateUsersPassword_Transaction(tenantIdentifierWithStorage.toAppIdentifier(), con,
finalLoginMethod.recipeUserId, passwordHash);
finalLoginMethod.getSupertokensUserId(), passwordHash);
return null;
});
return new ImportUserResponse(true, userInfoToBeUpdated);
Expand Down Expand Up @@ -618,7 +618,7 @@ public static void updateUsersEmailOrPassword(AppIdentifierWithStorage appIdenti
email);

for (AuthRecipeUserInfo userWithSameEmail : existingUsersWithNewEmail) {
if (userWithSameEmail.isPrimaryUser && !userWithSameEmail.id.equals(user.id)) {
if (userWithSameEmail.isPrimaryUser && !userWithSameEmail.getSupertokensUserId().equals(user.getSupertokensUserId())) {
throw new StorageTransactionLogicException(
new EmailChangeNotAllowedException());
}
Expand Down Expand Up @@ -682,8 +682,8 @@ public static UserInfo getUserUsingId(AppIdentifierWithStorage appIdentifierWith
return null;
}
for (LoginMethod lM : result.loginMethods) {
if (lM.recipeUserId.equals(userId)) {
return new UserInfo(lM.recipeUserId, result.isPrimaryUser, lM);
if (lM.getSupertokensUserId().equals(userId)) {
return new UserInfo(lM.getSupertokensUserId(), result.isPrimaryUser, lM);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ public static AuthRecipeUserInfo[] getUsers(Start start, TenantIdentifier tenant
// usersFromQuery
Map<String, AuthRecipeUserInfo> userIdToInfoMap = new HashMap<>();
for (AuthRecipeUserInfo user : users) {
userIdToInfoMap.put(user.id, user);
userIdToInfoMap.put(user.getSupertokensUserId(), user);
}
for (int i = 0; i < usersFromQuery.size(); i++) {
if (finalResult[i] == null) {
Expand Down Expand Up @@ -1155,7 +1155,7 @@ private static List<AuthRecipeUserInfo> getPrimaryUserInfoForUserIds(Start start

Map<String, LoginMethod> recipeUserIdToLoginMethodMap = new HashMap<>();
for (LoginMethod loginMethod : loginMethods) {
recipeUserIdToLoginMethodMap.put(loginMethod.recipeUserId, loginMethod);
recipeUserIdToLoginMethodMap.put(loginMethod.getSupertokensUserId(), loginMethod);
}

Map<String, AuthRecipeUserInfo> userIdToAuthRecipeUserInfo = new HashMap<>();
Expand Down
Loading
Loading