aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.44k stars 2.13k forks source link

Auth autoSignIn producing: Unknown: An unknown error has occurred. #13200

Closed JyTey2004 closed 7 months ago

JyTey2004 commented 7 months ago

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Authentication

Amplify Version

v6

Amplify Categories

auth

Backend

Amplify CLI

Environment information

System: OS: Windows 11 10.0.22621 CPU: (16) x64 AMD Ryzen 9 5900HS with Radeon Graphics Memory: 15.77 GB / 31.41 GB Binaries: Node: 20.12.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD npm: 10.5.0 - ~\AppData\Roaming\npm\npm.CMD Browsers: Edge: Chromium (122.0.2365.92) Internet Explorer: 11.0.22621.1 npmPackages: @aws-amplify/react-native: ^1.0.26 => 1.0.26 @babel/core: ^7.20.0 => 7.24.3 @react-native-async-storage/async-storage: 1.21.0 => 1.21.0 @react-native-community/netinfo: 11.1.0 => 11.1.0 @react-navigation/native: ^6.1.17 => 6.1.17 @react-navigation/stack: ^6.3.29 => 6.3.29 HelloWorld: 0.0.1 aws-amplify: ^6.0.25 => 6.0.25 aws-amplify/adapter-core: undefined () aws-amplify/analytics: undefined () aws-amplify/analytics/kinesis: undefined () aws-amplify/analytics/kinesis-firehose: undefined () aws-amplify/analytics/personalize: undefined () aws-amplify/analytics/pinpoint: undefined () aws-amplify/api: undefined () aws-amplify/api/server: undefined () aws-amplify/auth: undefined () aws-amplify/auth/cognito: undefined () aws-amplify/auth/cognito/server: undefined () aws-amplify/auth/enable-oauth-listener: undefined () aws-amplify/auth/server: undefined () aws-amplify/data: undefined () aws-amplify/data/server: undefined () aws-amplify/datastore: undefined () aws-amplify/in-app-messaging: undefined () aws-amplify/in-app-messaging/pinpoint: undefined () aws-amplify/push-notifications: undefined () aws-amplify/push-notifications/pinpoint: undefined () aws-amplify/storage: undefined () aws-amplify/storage/s3: undefined () aws-amplify/storage/s3/server: undefined () aws-amplify/storage/server: undefined () aws-amplify/utils: undefined () expo: ~50.0.14 => 50.0.14 expo-app-loading: ^2.1.1 => 2.1.1 expo-splash-screen: ~0.26.4 => 0.26.4 (0.17.5) expo-status-bar: ~1.11.1 => 1.11.1 react: 18.2.0 => 18.2.0 react-native: 0.73.6 => 0.73.6 react-native-gesture-handler: 2.14.0 => 2.14.0 react-native-get-random-values: 1.8.0 => 1.8.0 react-native-safe-area-context: 4.8.2 => 4.8.2 react-native-size-matters: ^0.4.2 => 0.4.2 react-native-vector-icons: ^10.0.3 => 10.0.3 npmGlobalPackages: @aws-amplify/cli: 12.10.3 @expo/ngrok: 4.1.1 @sanity/cli: 3.11.3 eas-cli: 5.9.3 expo-cli: 6.3.10 lambda-local: 2.1.2 nodemon: 2.0.15 npm: 10.5.0 react-native-cli: 2.0.1 serverless: 3.38.0 yarn: 1.22.19

Describe the bug

The below code was my authentication confirmation with auto sign in from aws-amplify/auth Followed the docs in aws-amplify v6 docs


const handleConfirmSignUp = async () => {
        try {
            const { isSignUpComplete, nextStep } = await confirmSignUp({
                username: email,
                confirmationCode: confirmationCode,
            });
            console.log('isSignUpComplete', isSignUpComplete);
            console.log('nextStep', nextStep);
            if (isSignUpComplete) {
                await handleAutoSignIn()
              }
        } catch (error) {
            console.log('error confirming sign up', error);
            setErrorMsg(error.message);
            setShowError(true);
        }
    }

    const handleAutoSignIn = async () => {
        try {
            const { isSignedIn } = await autoSignIn({});
            console.log('isSignedIn', isSignedIn);
            navigation.navigate('KYCScreen', { email: email, userId: userId });
        } catch (error) {
            console.log('error auto signing in', error);
            setErrorMsg('Error auto signing in.');
            setShowError(true);
        }
    }

Here are the logs and error: LOG isSignUpComplete true LOG nextStep {"signUpStep": "COMPLETE_AUTO_SIGN_IN"} LOG error auto signing in [Unknown: An unknown error has occurred.]

Expected behavior

The user should be auto signed in

Reproduction steps

  1. try the full authentication flow with autosignin after confirmSIgnUp

Code Snippet

// Put your code below this line.
const handleConfirmSignUp = async () => {
        try {
            const { isSignUpComplete, nextStep } = await confirmSignUp({
                username: email,
                confirmationCode: confirmationCode,
            });
            console.log('isSignUpComplete', isSignUpComplete);
            console.log('nextStep', nextStep);
            if (isSignUpComplete) {
                await handleAutoSignIn()
              }
        } catch (error) {
            console.log('error confirming sign up', error);
            setErrorMsg(error.message);
            setShowError(true);
        }
    }

    const handleAutoSignIn = async () => {
        try {
            const { isSignedIn } = await autoSignIn({});
            console.log('isSignedIn', isSignedIn);
            navigation.navigate('KYCScreen', { email: email, userId: userId });
        } catch (error) {
            console.log('error auto signing in', error);
            setErrorMsg('Error auto signing in.');
            setShowError(true);
        }
    }

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

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

cwomack commented 7 months ago

Hello, @JyTey2004 and thanks for opening this issue. I'll work on reproducing this on my side, but did this only just start happening after updating to the latest version of Amplify? Or is this a new app that you've just built?

JyTey2004 commented 7 months ago

Hello, @JyTey2004 and thanks for opening this issue. I'll work on reproducing this on my side, but did this only just start happening after updating to the latest version of Amplify? Or is this a new app that you've just built?

I have just updated to amplify v6. On v5, previously everything works fine, though the authentication flows were done on a react web app.

cwomack commented 7 months ago

@JyTey2004, can you confirm then you're only experiencing the issues then on mobile devices/emulators (both iOS and Android)? And are able to call signIn() API normally (without autoSignIn)? Also curious if theres any way to expand on the error message you're seeing.

Looks like this could be related to #13208 that was just transferred from the amplify-ui repo as well.

JyTey2004 commented 7 months ago

@cwomack, I'm experiencing it only on my IOS emulator as I'm not working on android currently. I was able to call the signIn() API normally without autoSignIn(). I was also able to call the signUp() and confirmSignUp() and account creation was not a problem. Only after adding autoSignIn() was there an issue, the message was only this [Unknown: An unknown error has occurred.].

Here's my sign in code, I also want to point out that the below is required to login using username and password as the error will also be [Unknown: An unknown error has occurred.]

// this is required to login using username and password 
  options: {
      authFlowType: 'USER_PASSWORD_AUTH'
  }
const login = async () => {
        try {
            const { isSignedIn, nextStep } = await signIn({ 
                username: email,
                password: password,
                // this is required to login using username and password 
                options: {
                    authFlowType: 'USER_PASSWORD_AUTH'
                }
            });
            console.log('isSignedIn', isSignedIn);
            console.log('nextStep', nextStep);
            checkAuthState();
        } catch (error) {
            console.log('error signing in', error);
            setErrorMsg('Invalid email or password.');
            setShowError(true);
        }
    }
israx commented 7 months ago

hello @JyTey2004 . Can you call the signUp API as follows ?

      const response = await signUp({
        username: data.username,
        password: data.password,
        options: {
          userAttributes: {
            ...attributes
          },
          autoSignIn: {
            authFlowType: 'USER_PASSWORD_AUTH'
          },
        },
      });

I assume you are using expo go. Unfortunately expo go won't link the native modules needed by Amplify to signIn with the SRP protocol. And calling autoSignIn will default to the SRP authentication flow. The alternative is to override the authFlow used by autoSignIn. You can do so by following the example above.

JyTey2004 commented 7 months ago

Hi @cwomack @israx , just tried out the proposed solution and everything works as expected! I'll close the issue. Thank you so much for the prompt response and solution!

hello @JyTey2004 . Can you call the signUp API as follows ?

      const response = await signUp({
        username: data.username,
        password: data.password,
        options: {
          userAttributes: {
            ...attributes
          },
          autoSignIn: {
            authFlowType: 'USER_PASSWORD_AUTH'
          },
        },
      });

I assume you are using expo go. Unfortunately expo go won't link the native modules needed by Amplify to signIn with the SRP protocol. And calling autoSignIn will default to the SRP authentication flow. The alternative is to override the authFlow used by autoSignIn. You can do so by following the example above.

cwomack commented 7 months ago

Thanks for confirming, @JyTey2004 and glad to hear you're unblocked!