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 define your auth resources like described on Manage MFA Settings page.
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.
When called successfully, the signin APIs will return an AuthSignInResult. Inspect the nextStep property in the result to see if additional signin steps are required.
If the next step is confirmSignInWithSMSMFACode, Amplify Auth has sent the user a random code over SMS, and is waiting to find out if the user successfully received it. To handle this step, your app's UI must prompt the user to enter the code. After the user enters the code, your implementation must pass the value to Amplify Auth confirmSignIn API.
Note: the signin result also includes an AuthCodeDeliveryDetails member. It includes additional information about the code delivery such as the partial phone number of the SMS recipient.
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.
funcconfirmSignIn(totpCode:String)async{
do{
let signInResult =tryawaitAmplify.Auth.confirmSignIn(challengeResponse: totpCode)
if signInResult.isSignedIn {
print("Confirm sign in succeeded. The user is signed in.")
}else{
print("Confirm sign in succeeded.")
print("Next step: \(signInResult.nextStep)")
// Switch on the next step to take appropriate actions.
// If `signInResult.isSignedIn` is true, the next step
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.
funcconfirmSignInWithTOTPAsMFASelection()async{
do{
let signInResult =tryawaitAmplify.Auth.confirmSignIn(
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.
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 setup when you configured a custom sign in flow. To complete this step, you should prompt the user for the custom challenge answer, and pass the answer to the confirmSignIn API.
let signInResult =tryawaitAmplify.Auth.confirmSignIn(challengeResponse: challengeAnswerFromUser)
if signInResult.isSignedIn {
print("Confirm sign in succeeded. The user is signed in.")
}else{
print("Confirm sign in succeeded.")
print("Next step: \(signInResult.nextStep)")
// Switch on the next step to take appropriate actions.
// If `signInResult.isSignedIn` is true, the next step
// is 'done', and the user is now signed in.
}
}catchlet error asAuthError{
print("Confirm sign in failed \(error)")
}catch{
print("Unexpected error: \(error)")
}
}
Special Handling on confirmSignIn
During a confirmSignIn call if failAuthentication=true is returned by the Lambda function the session of the request gets invalidated by cognito, a NotAuthorizedException is returned and a new signIn call is expected via Amplify.Auth.signIn
Exception: notAuthorized{message=Failed since user is not authorized., cause=NotAuthorizedException(message=Invalid session for the user.), recoverySuggestion=Check whether the given values are correct and the user is authorized to perform the operation.}
If the next step is confirmSignInWithNewPassword, Amplify Auth requires a new password for the user before they can proceed. Prompt the user for a new password and pass it to the confirmSignIn API.
If you receive resetPassword, authentication flow could not proceed without resetting the password. The next step is to invoke resetPassword api and follow the reset password flow.
funcresetPassword(username:String)async{
do{
let resetPasswordResult =tryawaitAmplify.Auth.resetPassword(for: username)
If you receive confirmSignUp as a next step, sign up could not proceed without confirming user information such as email or phone number. The next step is to invoke the confirmSignUp API and follow the confirm signup flow.
funcconfirmSignUp(for username:String, with confirmationCode:String)async{
do{
let confirmSignUpResult =tryawaitAmplify.Auth.confirmSignUp(
for: username,
confirmationCode: confirmationCode
)
print("Confirm sign up result completed: \(confirmSignUpResult.isSignUpComplete)")
}catchlet error asAuthError{
print("An error occurred while confirming sign up \(error)")
Signin flow is complete when you get done. This 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.