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 1 commit
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
10 changes: 7 additions & 3 deletions src/main/java/io/supertokens/passwordless/Passwordless.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public static ConsumeCodeResponse consumeCode(TenantIdentifierWithStorage tenant
long timeJoined = System.currentTimeMillis();
user = passwordlessStorage.createUser(tenantIdentifierWithStorage, userId, consumedDevice.email,
consumedDevice.phoneNumber, timeJoined);
return new ConsumeCodeResponse(true, user);
return new ConsumeCodeResponse(true, user, consumedDevice.email, consumedDevice.phoneNumber);
} catch (DuplicateEmailException | DuplicatePhoneNumberException e) {
// Getting these would mean that between getting the user and trying creating it:
// 1. the user managed to do a full create+consume flow
Expand All @@ -441,7 +441,7 @@ public static ConsumeCodeResponse consumeCode(TenantIdentifierWithStorage tenant
removeCodesByPhoneNumber(tenantIdentifierWithStorage, loginMethod.phoneNumber);
}
}
return new ConsumeCodeResponse(false, user);
return new ConsumeCodeResponse(false, user, consumedDevice.email, consumedDevice.phoneNumber);
}

@TestOnly
Expand Down Expand Up @@ -764,10 +764,14 @@ public CreateCodeResponse(String deviceIdHash, String codeId, String deviceId, S
public static class ConsumeCodeResponse {
public boolean createdNewUser;
public AuthRecipeUserInfo user;
public String email;
public String phoneNumber;

public ConsumeCodeResponse(boolean createdNewUser, AuthRecipeUserInfo user) {
public ConsumeCodeResponse(boolean createdNewUser, AuthRecipeUserInfo user, String email, String phoneNumber) {
this.createdNewUser = createdNewUser;
this.user = user;
this.email = email;
this.phoneNumber = phoneNumber;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.supertokens.output.Logging;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo;
import io.supertokens.pluginInterface.authRecipe.LoginMethod;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifierWithStorage;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
Expand Down Expand Up @@ -98,6 +99,15 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
userJson.remove("tenantIds");
}
result.add("user", userJson);
if (getVersionFromRequest(req).greaterThanOrEqualTo(SemVer.v4_0)) {
for (LoginMethod loginMethod : user.loginMethods) {
if (loginMethod.recipeId.equals(RECIPE_ID.EMAIL_PASSWORD) && normalisedEmail.equals(loginMethod.email)) {
result.addProperty("recipeUserId", loginMethod.recipeUserId);
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}
}

super.sendJsonResponse(200, result, resp);

} catch (WrongCredentialsException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
}

result.add("user", userJson);
if (getVersionFromRequest(req).greaterThanOrEqualTo(SemVer.v4_0)) {
result.addProperty("recipeUserId", user.id);
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
}
super.sendJsonResponse(200, result, resp);

} catch (DuplicateEmailException e) {
Logging.debug(main, tenantIdentifier, Utils.exceptionStacktraceToString(e));
JsonObject result = new JsonObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.supertokens.passwordless.Passwordless.ConsumeCodeResponse;
import io.supertokens.passwordless.exceptions.*;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.authRecipe.LoginMethod;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
Expand All @@ -39,6 +40,7 @@
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

public class ConsumeCodeAPI extends WebserverAPI {

Expand Down Expand Up @@ -108,6 +110,16 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I

result.addProperty("createdNewUser", consumeCodeResponse.createdNewUser);
result.add("user", userJson);
if (getVersionFromRequest(req).greaterThanOrEqualTo(SemVer.v4_0)) {
for (LoginMethod loginMethod : consumeCodeResponse.user.loginMethods) {
if (loginMethod.recipeId.equals(RECIPE_ID.PASSWORDLESS)
&& Objects.equals(loginMethod.email, consumeCodeResponse.email)
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
&& Objects.equals(loginMethod.phoneNumber, consumeCodeResponse.phoneNumber)) {
result.addProperty("recipeUserId", loginMethod.recipeUserId);
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}
}

super.sendJsonResponse(200, result, resp);
} catch (RestartFlowException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.supertokens.emailpassword.exceptions.EmailChangeNotAllowedException;
import io.supertokens.multitenancy.exception.BadPermissionException;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.authRecipe.LoginMethod;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.thirdparty.ThirdParty;
Expand All @@ -36,6 +37,7 @@
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Objects;

public class SignInUpAPI extends WebserverAPI {

Expand Down Expand Up @@ -89,6 +91,16 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
}

result.add("user", userJson);
if (getVersionFromRequest(req).greaterThanOrEqualTo(SemVer.v4_0)) {
for (LoginMethod loginMethod : response.user.loginMethods) {
if (loginMethod.recipeId.equals(RECIPE_ID.THIRD_PARTY)
&& Objects.equals(loginMethod.thirdParty.id, thirdPartyId)
&& Objects.equals(loginMethod.thirdParty.userId, thirdPartyUserId)) {
result.addProperty("recipeUserId", loginMethod.recipeUserId);
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}
}
super.sendJsonResponse(200, result, resp);

} catch (StorageQueryException | TenantOrAppNotFoundException e) {
Expand Down Expand Up @@ -141,6 +153,16 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
}

result.add("user", userJson);
if (getVersionFromRequest(req).greaterThanOrEqualTo(SemVer.v4_0)) {
for (LoginMethod loginMethod : response.user.loginMethods) {
if (loginMethod.recipeId.equals(RECIPE_ID.THIRD_PARTY)
&& Objects.equals(loginMethod.thirdParty.id, thirdPartyId)
&& Objects.equals(loginMethod.thirdParty.userId, thirdPartyUserId)) {
result.addProperty("recipeUserId", loginMethod.recipeUserId);
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}
}
super.sendJsonResponse(200, result, resp);

} catch (StorageQueryException | TenantOrAppNotFoundException | BadPermissionException e) {
Expand Down
Loading
Loading