Page updated Nov 14, 2023

Multi-step sign-in

After a user has finished signup, they can proceed to sign in. Amplify Auth signin flows can be multi step processes. The required steps are determined by the configuration you provided when you ran amplify add auth. Depending on the configuration, you may need to call various APIs to finish authenticating a user's signin attempt. To identify the next step in a signin flow, inspect the nextStep parameter in the signin result.

New enumeration values

When Amplify adds a new enumeration value (e.g., a new enum class entry or sealed class subtype in Kotlin, or a new enum value in Swift/Dart/Kotlin), it will publish a new minor version of the Amplify Library. Plugins that switch over enumeration values should include default handlers (an else branch in Kotlin or a default statement in Swift/Dart/Kotlin) to ensure that they are not impacted by new enumeration values.

The Amplify.Auth.signIn API returns a SignInResult object which indicates whether the sign-in flow is complete or whether additional steps are required before the user is signed in.

To see if additional signin steps are required, inspect the sign in result's nextStep.signInStep property.

  • If the sign-in step is done, the flow is complete and the user is signed in.
  • If the sign-in step is not done, one or more additional steps are required. These are explained in detail below.

The signInStep property is an enum of type AuthSignInStep. Depending on its value, your code should take one of the actions mentioned on this page.

1Future<SignInResult> signInWithCognito(
2 String username,
3 String password,
4) async {
5 final SignInResult result = await Amplify.Auth.signIn(
6 username: username,
7 password: password,
8 );
9 return _handleSignInResult(result);
10}
11
12Future<void> _handleSignInResult(SignInResult result) async {
13 switch (result.nextStep.signInStep) {
14 case AuthSignInStep.continueSignInWithMfaSelection:
15 // Handle select from MFA methods case
16 case AuthSignInStep.continueSignInWithTotpSetup:
17 // Handle TOTP setup case
18 case AuthSignInStep.confirmSignInWithTotpMfaCode:
19 // Handle TOTP MFA case
20 case AuthSignInStep.confirmSignInWithSmsMfaCode:
21 // Handle SMS MFA case
22 case AuthSignInStep.confirmSignInWithNewPassword:
23 // Handle new password case
24 case AuthSignInStep.confirmSignInWithCustomChallenge:
25 // Handle custom challenge case
26 case AuthSignInStep.resetPassword:
27 // Handle reset password case
28 case AuthSignInStep.confirmSignUp:
29 // Handle confirm sign up case
30 case AuthSignInStep.done:
31 safePrint('Sign in is complete');
32 }
33}

Confirm signin with SMS MFA

If the next step is confirmSignInWithSmsMfaCode, Amplify Auth has sent the user a random code over SMS and is waiting for the user to verify that code. To handle this step, your app's UI must prompt the user to enter the code. After the user enters the code, pass the value to the confirmSignIn API.

The result includes an AuthCodeDeliveryDetails member. It includes additional information about the code delivery, such as the partial phone number of the SMS recipient, which can be used to prompt the user on where to look for the code.

1Future<void> _handleSignInResult(SignInResult result) async {
2 switch (result.nextStep.signInStep) {
3 case AuthSignInStep.confirmSignInWithSmsMfaCode:
4 final codeDeliveryDetails = result.nextStep.codeDeliveryDetails!;
5 _handleCodeDelivery(codeDeliveryDetails);
6 // ...
7 }
8}
9
10void _handleCodeDelivery(AuthCodeDeliveryDetails codeDeliveryDetails) {
11 safePrint(
12 'A confirmation code has been sent to ${codeDeliveryDetails.destination}. '
13 'Please check your ${codeDeliveryDetails.deliveryMedium.name} for the code.',
14 );
15}
1Future<void> confirmMfaUser(String mfaCode) async {
2 try {
3 final result = await Amplify.Auth.confirmSignIn(
4 confirmationValue: mfaCode,
5 );
6 return _handleSignInResult(result);
7 } on AuthException catch (e) {
8 safePrint('Error confirming MFA code: ${e.message}');
9 }
10}

Confirm signin with TOTP MFA

If the next step is confirmSignInWithTOTPCode, you should prompt the user to enter the TOTP code from their associated authenticator app during set up. The code is a six-digit number that changes every 30 seconds. The user must enter the code before the 30-second window expires.

After the user enters the code, your implementation must pass the value to Amplify Auth confirmSignIn API.

1Future<void> _handleSignInResult(SignInResult result) async {
2 switch (result.nextStep.signInStep) {
3 // ···
4 case AuthSignInStep.confirmSignInWithTotpMfaCode:
5 safePrint('Enter a one-time code from your registered authenticator app');
6 // ···
7 }
8}
9
10// Then, pass the TOTP code to `confirmSignIn`
11
12Future<void> confirmTotpUser(String totpCode) async {
13 try {
14 final result = await Amplify.Auth.confirmSignIn(
15 confirmationValue: totpCode,
16 );
17 return _handleSignInResult(result);
18 } on AuthException catch (e) {
19 safePrint('Error confirming TOTP code: ${e.message}');
20 }
21}

Continue signin with MFA Selection

If the next step is continueSignInWithMFASelection, the user must select the MFA method to use. Amplify Auth currently only supports SMS and TOTP as MFA methods. After the user selects an MFA method, your implementation must pass the selected MFA method to Amplify Auth using confirmSignIn API.

The MFA types which are currently supported by Amplify Auth are:

  • MfaType.sms
  • MfaType.totp
1Future<void> _handleSignInResult(SignInResult result) async {
2 switch (result.nextStep.signInStep) {
3 // ···
4 case AuthSignInStep.continueSignInWithMfaSelection:
5 final allowedMfaTypes = result.nextStep.allowedMfaTypes!;
6 final selection = await _promptUserPreference(allowedMfaTypes);
7 return _handleMfaSelection(selection);
8 // ···
9 }
10}
11
12Future<MfaType> _promptUserPreference(Set<MfaType> allowedTypes) async {
13 // ···
14}
15
16Future<void> _handleMfaSelection(MfaType selection) async {
17 try {
18 final result = await Amplify.Auth.confirmSignIn(
19 confirmationValue: selection.confirmationValue,
20 );
21 return _handleSignInResult(result);
22 } on AuthException catch (e) {
23 safePrint('Error resending code: ${e.message}');
24 }
25}

Continue signin with TOTP Setup

If the next step is continueSignInWithTOTPSetup, then the user must provide a TOTP code to complete the sign in process. The step returns an associated value of type TOTPSetupDetails which would be used for generating TOTP. TOTPSetupDetails provides a helper method called getSetupURI that can be used to generate a URI, which can be used by native password managers for TOTP association. For example. if the URI is used on Apple platforms, it will trigger the platform's native password manager to associate TOTP with the account. For more advanced use cases, TOTPSetupDetails also contains the sharedSecret that will be used to either generate a QR code or can be manually entered into an authenticator app.

Once the authenticator app is set up, the user can generate a TOTP code and provide it to the library to complete the sign in process.

1Future<void> _handleSignInResult(SignInResult result) async {
2 switch (result.nextStep.signInStep) {
3 // ···
4 case AuthSignInStep.continueSignInWithTotpSetup:
5 final totpSetupDetails = result.nextStep.totpSetupDetails!;
6 final setupUri = totpSetupDetails.getSetupUri(appName: 'MyApp');
7 safePrint('Open URI to complete setup: $setupUri');
8 // ···
9 }
10}
11
12// Then, pass the TOTP code to `confirmSignIn`
13
14Future<void> confirmTotpUser(String totpCode) async {
15 try {
16 final result = await Amplify.Auth.confirmSignIn(
17 confirmationValue: totpCode,
18 );
19 return _handleSignInResult(result);
20 } on AuthException catch (e) {
21 safePrint('Error confirming TOTP code: ${e.message}');
22 }
23}

Confirm signin with custom challenge

If the next step is confirmSignInWithCustomChallenge, Amplify Auth is awaiting completion of a custom authentication challenge. The challenge is based on the Lambda trigger you configured as part of a custom sign in flow.

For example, your custom challenge Lambda may pass a prompt to the frontend which requires the user to enter a secret code.

1Future<void> _handleSignInResult(SignInResult result) async {
2 switch (result.nextStep.signInStep) {
3 // ...
4 case AuthSignInStep.confirmSignInWithCustomChallenge:
5 final parameters = result.nextStep.additionalInfo;
6 final hint = parameters['hint']!;
7 safePrint(hint); // "Enter the secret code"
8 // ...
9 }
10}

To complete this step, you should prompt the user for the custom challenge answer, and pass the answer to the confirmSignIn API.

1Future<void> confirmCustomChallenge(String answer) async {
2 try {
3 final result = await Amplify.Auth.confirmSignIn(
4 confirmationValue: answer,
5 );
6 return _handleSignInResult(result);
7 } on AuthException catch (e) {
8 safePrint('Error confirming custom challenge: ${e.message}');
9 }
10}
Special Handling on confirmSignIn

If failAuthentication=true is returned by the Lambda, Cognito will invalidate the session of the request. This is represented by a NotAuthorizedException and requires restarting the sign-in flow by calling Amplify.Auth.signIn again.

Confirm signin with new password

If the next step is confirmSignInWithNewPassword, Amplify Auth requires the user choose a new password they proceeding with the sign in.

Prompt the user for a new password and pass it to the confirmSignIn API.

1Future<void> _handleSignInResult(SignInResult result) async {
2 switch (result.nextStep.signInStep) {
3 // ...
4 case AuthSignInStep.confirmSignInWithNewPassword:
5 safePrint('Please enter a new password');
6 // ...
7 }
8}
1Future<void> confirmNewPassword(String newPassword) async {
2 try {
3 final result = await Amplify.Auth.confirmSignIn(
4 confirmationValue: newPassword,
5 );
6 return _handleSignInResult(result);
7 } on AuthException catch (e) {
8 safePrint('Error confirming new password: ${e.message}');
9 }
10}

Reset password

If the next step is resetPassword, Amplify Auth requires that the user reset their password before proceeding. Use the resetPassword API to guide the user through resetting their password, then call Amplify.Auth.signIn when that's complete to restart the sign-in flow.

See the reset password docs for more information.

1Future<void> _handleSignInResult(SignInResult result) async {
2 switch (result.nextStep.signInStep) {
3 // ...
4 case AuthSignInStep.resetPassword:
5 final resetResult = await Amplify.Auth.resetPassword(
6 username: username,
7 );
8 await _handleResetPasswordResult(resetResult);
9 // ...
10 }
11}
12
13Future<void> _handleResetPasswordResult(ResetPasswordResult result) async {
14 switch (result.nextStep.updateStep) {
15 case AuthResetPasswordStep.confirmResetPasswordWithCode:
16 final codeDeliveryDetails = result.nextStep.codeDeliveryDetails!;
17 _handleCodeDelivery(codeDeliveryDetails);
18 case AuthResetPasswordStep.done:
19 safePrint('Successfully reset password');
20 }
21}
22
23void _handleCodeDelivery(AuthCodeDeliveryDetails codeDeliveryDetails) {
24 safePrint(
25 'A confirmation code has been sent to ${codeDeliveryDetails.destination}. '
26 'Please check your ${codeDeliveryDetails.deliveryMedium.name} for the code.',
27 );
28}

Confirm Signup

If the next step is resetPassword, Amplify Auth requires that the user confirm their email or phone number before proceeding. Use the resendSignUpCode API to send a new sign up code to the registered email or phone number, followed by confirmSignUp to complete the sign up.

See the confirm sign up docs for more information.

The result includes an AuthCodeDeliveryDetails member. It includes additional information about the code delivery, such as the partial phone number of the SMS recipient, which can be used to prompt the user on where to look for the code.

1Future<void> _handleSignInResult(SignInResult result) async {
2 switch (result.nextStep.signInStep) {
3 // ...
4 case AuthSignInStep.confirmSignUp:
5 // Resend the sign up code to the registered device.
6 final resendResult = await Amplify.Auth.resendSignUpCode(
7 username: username,
8 );
9 _handleCodeDelivery(resendResult.codeDeliveryDetails);
10 // ...
11 }
12}
13
14void _handleCodeDelivery(AuthCodeDeliveryDetails codeDeliveryDetails) {
15 safePrint(
16 'A confirmation code has been sent to ${codeDeliveryDetails.destination}. '
17 'Please check your ${codeDeliveryDetails.deliveryMedium.name} for the code.',
18 );
19}
1Future<void> confirmSignUp({
2 required String username,
3 required String confirmationCode,
4}) async {
5 try {
6 await Amplify.Auth.confirmSignUp(
7 username: username,
8 confirmationCode: confirmationCode,
9 );
10 } on AuthException catch (e) {
11 safePrint('Error confirming sign up: ${e.message}');
12 }
13}

Once the sign up is confirmed, call Amplify.Auth.signIn again to restart the sign-in flow.

Done

The sign-in flow is complete when the next step is done, which means the user is successfully authenticated. As a convenience, the SignInResult also provides the isSignedIn property, which will be true if the next step is done.

1Future<void> _handleSignInResult(SignInResult result) async {
2 switch (result.nextStep.signInStep) {
3 // ...
4 case AuthSignInStep.done:
5 // Could also check that `result.isSignedIn` is `true`
6 safePrint('Sign in is complete');
7 }
8}