amplify-js: React Native - signIn [Unknown: An unknown error has ocurred.]

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Authentication

Amplify Categories

auth

Environment information

# Put output below this line
@aws-amplify/react-native => 1.0.3
aws-amplify => 6.0.3

Describe the bug

getting error [Unknown: An unknown error has ocurred.] only when calling signIn

Expected behavior

signIn to work

Reproduction steps

call signIn

Code Snippet

// Put your code below this line.

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

About this issue

  • Original URL
  • State: closed
  • Created 7 months ago
  • Reactions: 2
  • Comments: 37 (15 by maintainers)

Most upvoted comments

I’m having the same problem after testing some tips proposed here.

“An unknown error has occurred” at the time of login, with account creation and email verification carried out successfully.

import { StatusBar } from 'expo-status-bar';
import RootNavigator from './src/navigation';

import { NavigationContainer } from '@react-navigation/native';
import { Amplify } from 'aws-amplify';
import {  withAuthenticator } from '@aws-amplify/ui-react-native';
import awsExports  from './src/aws-exports';

Amplify.configure(awsExports);

function App() {
  return (
    <NavigationContainer>
      <RootNavigator />

      <StatusBar style="light" />
    </NavigationContainer>
  );
};

export default withAuthenticator(App);

Captura de tela 2024-04-06 162224

I tried using expo go on my iPhone, I used expo go on an android emulator, and none of them work

Thanks guys, we were using nx monorepo, it seemed we need to import react-native-get-random-values to the package.json for our app.

"aws-amplify": "*",
"@react-native-async-storage/async-storage": "*",
"@aws-amplify/react-native": "*",
"react-native-get-random-values": "*"

@steve-calverley

feel free to open a new issue but I am still not sure why setTokens is failing, I wonder if there is something related to async storage configuration. Are you running the App on iOS, or Android?

@elorzafe Running on both iOS and Android emulators. Let me look into Async Config before opening new issue

@israx but in a clean expo49 project with expo go, works. I only get that error when I try in a monorepo…

Hi @elorzafe sorry to bother you again, in my clean expo49 it works but then when I do the same thing in my monorepo I get this error right on openning the app (app doesnt work):

ERROR Error: The package ‘@aws-amplify/react-native’ doesn’t seem to be linked. Make sure:

  • You rebuilt the app after installing the package
  • You are not using Expo Go , js engine: hermes ERROR Invariant Violation: “main” has not been registered. This can happen if:
  • Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
  • A module failed to load due to an error and AppRegistry.registerComponent wasn’t called., js engine: hermes

I have “@aws-amplify/react-native”: “^1.0.4”, installed.

If I go to the version that I had before, “aws-amplify”: “4.3.43” it works fine…

@joaobsantos

On the code that you shared I think the signIn invocation should have the options: { authFlowType: '...' } not handleSignIn. Also can you share how you are importing and configuring (redacted) the library?

@elorzafe sorry, you are right, and now it works… Maybe improve the docs to have the authFlowType: “USER_PASSWORD_AUTH” https://docs.amplify.aws/react-native/build-a-backend/auth/enable-sign-up/#sign-in

Thx for your time to help me I really apreciate and keep up the good work.

async function handleSignIn({ username, password }: SignInInput) {
    try {
      const user = await signIn({
        username,
        password,
        options: {
          authFlowType: "USER_PASSWORD_AUTH",
        },
      });
      console.log("user", user);
    } catch (error) {
      //@ts-ignore
      console.log("Error signing in:", error.underlyingError);
    }
  } 
  
  

@steve-calverley

Can you shared a redacted example of how you are importing, configuring and calling the APIs?

@elorzafe two functions we use - very rough for testing this issue

 const signIn = async (username: string) => {
    try {
      const signInResult = await AWSSignIn({
        username,
        options: {
          authFlowType: 'CUSTOM_WITHOUT_SRP',
        },
      });

      console.log('signInResult', signInResult);
      const {isSignedIn, nextStep} = signInResult;

      if (isSignedIn) {
        return initSignIn();
      }

      switch (nextStep.signInStep) {
        case 'CONFIRM_SIGN_IN_WITH_CUSTOM_CHALLENGE':
          break;
        case 'CONFIRM_SIGN_IN_WITH_NEW_PASSWORD_REQUIRED':
          // return signInResult;
          break;
        case 'RESET_PASSWORD':
          break;
        default:
          console.log(nextStep.signInStep);
          throw new Error('Not a Sign In Event we handle');
      }
    } catch (error: unknown) {
      console.log('ERROR::useCognitoUser::signIn:: ', error);
      throw error;
    }
  };

  const sendCustomChallengeAnswer = async (
    answer: string,
  ): Promise<AuthUser | any> => {
    let user;
    try {
      console.log('answer', answer);

      const confirmSignInOutput = await AWSConfirmSignIn({
        challengeResponse: answer,
      });
      console.log('confirmSignInOutput', confirmSignInOutput);
      if (!confirmSignInOutput.isSignedIn) {
        throw new Error('Incorrect Answer. Please try again');
      }
      user = await getCurrentUser();
      console.log('user', user);

      return user;
    } catch (error: unknown) {
      if (!user) {
        console.log(
          'ERROR::useCognitoUser::sendCustomChallengeAnswer:: ',
          error,
        );
        throw new Error('Unable to sign in');
      }
    }
  };

First function is called on screen that obtains phone number -> move to send screen to enter answer -> 2nd function is called.

Config

import amplifyconfig from './src/amplifyconfiguration.json';
Amplify.configure(amplifyconfig);

@joaobsantos

On the code that you shared I think the signIn invocation should have the options: { authFlowType: '...' } not handleSignIn. Also can you share how you are importing and configuring (redacted) the library?

Hi @mtergel can you confirm that you have installed react-native-get-random-values in your project?

Error signing in: [Error: The package ‘@aws-amplify/react-native’ doesn’t seem to be linked. Make sure:

  • You rebuilt the app after installing the package
  • You are not using Expo Go

I have “@aws-amplify/react-native”: “^1.0.4”, installed…

Sorry my mistake. It should be error.underlyingError , apologies.

Something I forgot @joaobsantos , just for testing purposes only. Can you call the signIn API as follows.

await signIn({
	username: 'your_username',
	password: 'your_password',
	options: {
		authFlowType: 'USER_PASSWORD_AUTH',
	},
});

This will change the actual auth flow type. However we recommend using the default flow which is SRP. Let me know if that ends up working please.

Hi guys I’m also getting this error, I’ve installed a fresh expo49 app and only the aws amplify to test it, and I got this error signing in [Unknown: An unknown error has ocurred.]

Also today I’ve tried the new versions: “@aws-amplify/react-native”: “^1.0.4” “aws-amplify”: “^6.0.4”, “react-native-get-random-values”: “1.10.0”

Still the same problem. Thx

Im also getting this on the confirmSignIn after a successful challenge response - working fine in v5 and no change to CUSTOM_CHALLENGE signIn returns the expected Output confirmSignInOutput {"isSignedIn": false, "nextStep": {"additionalInfo": {"attempts": "1", "attemptsLeft": "2", "maxAttempts": "3", "phone_number": "+61********"}, "signInStep": "CONFIRM_SIGN_IN_WITH_CUSTOM_CHALLENGE"}} But passing correct challenge will throw Unkown Error