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

PreSignUp Trigger error not forwarding to signInWithRedirects_failure #14063

Closed
3 tasks done
bilalAlgoace opened this issue Dec 10, 2024 · 6 comments
Closed
3 tasks done
Assignees
Labels
Auth Related to Auth components/category Hub Related to Hub category Next.js question General question

Comments

@bilalAlgoace
Copy link

Before opening, please confirm:

JavaScript Framework

Next.js

Amplify APIs

Authentication

Amplify Version

v6

Amplify Categories

auth

Backend

Amplify CLI

Environment information

# Put output below this line

npm run dev

Describe the bug

I have created a preSignUp trigger which checks whether the email is registered or not and if registered then do not allow user to register that email this is working fine with simple email and password process and throwing error of user already exists but with i am using signInWithRedirects it is showing error in search params like this:

http://localhost:3000/signup?error_description=PreSignUp+failed+with+error+User+already+exists..+&state=lymVlSL5AV0pIeORuFFVDdSe1r3Wh7zx&error=invalid_request

but not forwarding any error in signInWIthRedirects_failure

I am using Next js 14 app router

but this code is working in Reavt Native and showing proper error in signInWIthRedirects_failure

Expected behavior

It should show error in signInWIthRedirects_failure

Reproduction steps

I am using signInWithRedirects for social logins with amplify

Code Snippet

// Put your code below this line.
My Code:

useEffect(() => {
    const unsubscribe = Hub.listen("auth", ({ payload }) => {
      console.log("auth", payload)
      switch (payload.event) {
        case "signedIn":
          console.log("HUB SIGNED IN");
          // getUserData();
          isUserExist(setUser);
          break;
        case "signedOut":
          console.log("HUB SIGNED OUT");
          break; 
        case "signInWithRedirect":
          console.log("case signInWithRedirect")
          // getUserData();
          isUserExist(setUser);
          break;
        case "signInWithRedirect_failure":
          setError("An error has occurred during the OAuth flow.");
          console.log("Error")
          break;
      }
    });
    console.log("out of switch case")

    // getUserData();

    return unsubscribe;
  }, []);
  
  
  function for social logins: 
  
    const handleSocialLogin = async (provider: AuthProvider) => {
    try {
      const res = await signInWithRedirect({ provider: provider});
      console.log("🚀 ~ handleSocialLogin ~ res:", res);
    } catch (error) {
      console.error("Error with social login: ", error);
    }
  };

Log output

// Put your logs below this line


aws-exports.js

const awsmobile = {
"aws_project_region": "us-east-2",
"aws_cognito_identity_pool_id": "us-east-2xxxxxxxxxxxxxxx",
"aws_cognito_region": "us-east-2",
"aws_user_pools_id": "us-east-2_xxxxxxxxxxxxxxxxx",
"aws_user_pools_web_client_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"oauth": {
"domain": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"scope": [
"phone",
"email",
"openid",
"profile",
"aws.cognito.signin.user.admin"
],
"redirectSignIn": "kwelta://,http://localhost:3000/signup",
"redirectSignOut": "kwelta://,http://localhost:3000/signin",
"responseType": "code"
},
"federationTarget": "COGNITO_USER_POOLS",
"aws_cognito_username_attributes": [
"EMAIL"
],
"aws_cognito_social_providers": [
"FACEBOOK",
"GOOGLE",
"APPLE"
],
"aws_cognito_signup_attributes": [
"EMAIL"
],
"aws_cognito_mfa_configuration": "OFF",
"aws_cognito_mfa_types": [
"SMS"
],
"aws_cognito_password_protection_settings": {
"passwordPolicyMinLength": 8,
"passwordPolicyCharacters": []
},
"aws_cognito_verification_mechanisms": [
"EMAIL"
]
};

export default awsmobile;

Manual configuration

{
"aws_project_region": "us-east-2",
"aws_cognito_identity_pool_id": "us-east-2xxxxxxxxxxxxxxxxxxxxxxx",
"aws_cognito_region": "us-east-2",
"aws_user_pools_id": "us-east-2_xxxxxxxxxxxxx",
"aws_user_pools_web_client_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"oauth": {
"domain": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"scope": [
"phone",
"email",
"openid",
"profile",
"aws.cognito.signin.user.admin"
],
"redirectSignIn": "kwelta://,http://localhost:3000/signup",
"redirectSignOut": "kwelta://,http://localhost:3000/signin",
"responseType": "code"
},
"federationTarget": "COGNITO_USER_POOLS",
"aws_cognito_authentication_flow_type": "CUSTOM_AUTH",
"aws_cognito_username_attributes": [
"EMAIL"
],
"aws_cognito_social_providers": [
"FACEBOOK",
"GOOGLE",
"APPLE"
],
"aws_cognito_signup_attributes": [
"EMAIL"
],
"aws_cognito_mfa_configuration": "OFF",
"aws_cognito_mfa_types": [
"SMS"
],
"aws_cognito_password_protection_settings": {
"passwordPolicyMinLength": 8,
"passwordPolicyCharacters": []
},
"aws_cognito_verification_mechanisms": [
"EMAIL"
]
}

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

@github-actions github-actions bot added pending-triage Issue is pending triage pending-maintainer-response Issue is pending a response from the Amplify team. labels Dec 10, 2024
@cwomack cwomack self-assigned this Dec 10, 2024
@cwomack cwomack added Auth Related to Auth components/category Next.js Hub Related to Hub category labels Dec 10, 2024
@cwomack
Copy link
Member

cwomack commented Dec 10, 2024

Hello, @bilalAlgoace 👋. This looks similar to what's being captured in issue #13436 where there's possibly a race condition where the Hub event is dispatched before the useEffect hook is called. Can you see the advice within this comment to see if moving the Amplify.configure() call within the useEffect hook resolves the issue?

@github-actions github-actions bot removed the pending-maintainer-response Issue is pending a response from the Amplify team. label Dec 10, 2024
@cwomack cwomack added question General question pending-community-response Issue is pending a response from the author or community. and removed pending-triage Issue is pending triage labels Dec 10, 2024
@bilalAlgoace
Copy link
Author

@cwomack Thank you it works for me. 👍

@github-actions github-actions bot added pending-maintainer-response Issue is pending a response from the Amplify team. and removed pending-community-response Issue is pending a response from the author or community. labels Dec 11, 2024
@cwomack
Copy link
Member

cwomack commented Dec 11, 2024

Glad to hear, @bilalAlgoace and thanks for following up!

@github-actions github-actions bot removed the pending-maintainer-response Issue is pending a response from the Amplify team. label Dec 11, 2024
@bilalAlgoace bilalAlgoace reopened this Dec 11, 2024
@bilalAlgoace
Copy link
Author

@cwomack now i am facing a issue on social authentications i am trying to sigin with google it is throw error:

{
"event": "signInWithRedirect_failure",
"data": {
"error": {
"name": "OAuthSignInException",
"recoverySuggestion": "Make sure Cognito Hosted UI has been configured correctly"
}
}
}

error message : "User cancelled OAuth flow."

The reason I think is that in cogtino my redirect callbackurl is: http://localhost:3000/ and I have added a protection layer in my app that if user is not authenticated it should get redirected to http://localhost:3000/login.

That's why it is showing this: "User cancelled OAuth flow" error.

but It is only happening with social logins and if i am using normal signin with email password it is working perfect.

and If I changed my cogtino redirect callbackurl is: http://localhost:3000/login which doesn't need any authentication in my app it is also working properly and is signing in normally with social logins too.

@cwomack if you can please guide me in this too.

@github-actions github-actions bot added the pending-maintainer-response Issue is pending a response from the Amplify team. label Dec 11, 2024
@cwomack
Copy link
Member

cwomack commented Dec 11, 2024

@bilalAlgoace, it's possible that the added protection layer is impacting the behavior here when returning back from the Cognito Hosted UI. Can you provide a little more detail as to what's happening there (or share the relevant code)?

But you should be able to keep using the redirect callback URL of http://localhost:3000/login (which should avoid the auth protection step) and then wait for the user session to be available via a call to the fetchAuthSession API before redirecting back to the home page or listen to the signedIn event with the Hub utility.

@cwomack cwomack added pending-community-response Issue is pending a response from the author or community. and removed pending-maintainer-response Issue is pending a response from the Amplify team. labels Dec 11, 2024
@bilalAlgoace
Copy link
Author

bilalAlgoace commented Dec 12, 2024

@cwomack yeah I did the same thing you are telling in the comment and it is working perfectly.
Thank You. 👍

@github-actions github-actions bot added pending-maintainer-response Issue is pending a response from the Amplify team. and removed pending-community-response Issue is pending a response from the author or community. pending-maintainer-response Issue is pending a response from the Amplify team. labels Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auth Related to Auth components/category Hub Related to Hub category Next.js question General question
Projects
None yet
Development

No branches or pull requests

2 participants