Sign-in
Amplify provides a client library that enables you to interact with backend resources such as Amplify Auth.
Using the signIn API
Amplify.Auth.signIn( "username", "password", result -> Log.i("AuthQuickstart", result.isSignedIn() ? "Sign in succeeded" : "Sign in not complete"), error -> Log.e("AuthQuickstart", error.toString()));
Amplify.Auth.signIn("username", "password", { result -> if (result.isSignedIn) { Log.i("AuthQuickstart", "Sign in succeeded") } else { Log.i("AuthQuickstart", "Sign in not complete") } }, { Log.e("AuthQuickstart", "Failed to sign in", it) })
try { val result = Amplify.Auth.signIn("username", "password") if (result.isSignedIn) { Log.i("AuthQuickstart", "Sign in succeeded") } else { Log.e("AuthQuickstart", "Sign in not complete") }} catch (error: AuthException) { Log.e("AuthQuickstart", "Sign in failed", error)}
RxAmplify.Auth.signIn("username", "password") .subscribe( result -> Log.i("AuthQuickstart", result.isSignedIn() ? "Sign in succeeded" : "Sign in not complete"), error -> Log.e("AuthQuickstart", error.toString()) );
The signIn
API response will include a nextStep
property, which can be used to determine if further action is required. It may return the following next steps:
CONFIRM_SIGN_IN_WITH_NEW_PASSWORD_REQUIRED
- The user was created with a temporary password and must set a new one. Complete the process withconfirmSignIn
.CONFIRM_SIGN_IN_WITH_CUSTOM_CHALLENGE
- The sign-in must be confirmed with a custom challenge response. Complete the process withconfirmSignIn
.CONFIRM_SIGN_IN_WITH_TOTP_CODE
- The sign-in must be confirmed with a TOTP code from the user. Complete the process withconfirmSignIn
.CONTINUE_SIGN_IN_WITH_TOTP_SETUP
- The TOTP setup process must be continued. Complete the process withconfirmSignIn
.CONFIRM_SIGN_IN_WITH_SMS_CODE
- The sign-in must be confirmed with a SMS code from the user. Complete the process withconfirmSignIn
.CONTINUE_SIGN_IN_WITH_MFA_SELECTION
- The user must select their mode of MFA verification before signing in. Complete the process withconfirmSignIn
.RESET_PASSWORD
- The user must reset their password viaresetPassword
.CONFIRM_SIGN_UP
- The user hasn't completed the sign-up flow fully and must be confirmed viaconfirmSignUp
.DONE
- The sign in process has been completed.
For more information on handling the TOTP and MFA steps that may be returned, see multi-factor authentication.
Confirm sign-in
With multi-factor auth enabled
When multi-factor authentication (MFA) is required with SMS in your backend auth resource, you will need to pass the phone number during sign-up API call. If you are using the email
or username
as the primary sign-in mechanism, you will need to pass the phone_number
attribute as a user attribute. This will change depending on if you enable SMS, TOTP, or both. Visit the multi-factor authentication documentation to learn more about enabling MFA on your backend auth resource.
ArrayList<AuthUserAttribute> attributes = new ArrayList<>();attributes.add(new AuthUserAttribute(AuthUserAttributeKey.email(), "my@email.com"));attributes.add(new AuthUserAttribute(AuthUserAttributeKey.phoneNumber(), "+15551234567"));
Amplify.Auth.signUp( "username", "Password123", AuthSignUpOptions.builder().userAttributes(attributes).build(), result -> Log.i("AuthQuickstart", result.toString()), error -> Log.e("AuthQuickstart", error.toString()));
val attrs = mapOf( AuthUserAttributeKey.email() to "my@email.com", AuthUserAttributeKey.phoneNumber() to "+15551234567")val options = AuthSignUpOptions.builder() .userAttributes(attrs.map { AuthUserAttribute(it.key, it.value) }) .build()Amplify.Auth.signUp("username", "Password123", options, { Log.i("AuthQuickstart", "Sign up result = $it") }, { Log.e("AuthQuickstart", "Sign up failed", it) })
val attrs = mapOf( AuthUserAttributeKey.email() to "my@email.com", AuthUserAttributeKey.phoneNumber() to "+15551234567")val options = AuthSignUpOptions.builder() .userAttributes(attrs.map { AuthUserAttribute(it.key, it.value) }) .build()try { val result = Amplify.Auth.signUp("username", "Password123", options) Log.i("AuthQuickstart", "Sign up OK: $result")} catch (error: AuthException) { Log.e("AuthQuickstart", "Sign up failed", error)}
ArrayList<AuthUserAttribute> attributes = new ArrayList<>();attributes.add(new AuthUserAttribute(AuthUserAttributeKey.email(), "my@email.com"));attributes.add(new AuthUserAttribute(AuthUserAttributeKey.phoneNumber(), "+15551234567"));
RxAmplify.Auth.signUp( "username", "Password123", AuthSignUpOptions.builder().userAttributes(attributes).build()) .subscribe( result -> Log.i("AuthQuickstart", result.toString()), error -> Log.e("AuthQuickstart", error.toString()) );
You will then confirm sign-up, sign in, and receive a nextStep
in the sign-in result of type CONFIRM_SIGN_IN_WITH_SMS_MFA_CODE
. A confirmation code will also be texted to the phone number provided above. Pass the code you received to the confirmSignIn
API:
Amplify.Auth.confirmSignIn( "confirmation code received via SMS", result -> Log.i("AuthQuickstart", result.toString()), error -> Log.e("AuthQuickstart", error.toString()));
Amplify.Auth.confirmSignIn("code received via SMS", { Log.i("AuthQuickstart", "Confirmed signin: $it") }, { Log.e("AuthQuickstart", "Failed to confirm signin", it) })
try { val result = Amplify.Auth.confirmSignIn("code received via SMS") Log.i("AuthQuickstart", "Confirmed signin: $result") } catch (error: AuthException) { Log.e("AuthQuickstart", "Failed to confirm signin", error)}
RxAmplify.Auth.confirmSignIn("confirmation code received via SMS") .subscribe( result -> Log.i("AuthQuickstart", result.toString()), error -> Log.e("AuthQuickstart", error.toString()) );
Sign in with an external identity provider
To sign in using an external identity provider such as Google, use the signInWithRedirect
function.
Update AndroidManifest.xml
Add the following activity and queries tag to your app's AndroidManifest.xml
file, replacing myapp
with
your redirect URI prefix if necessary:
<application ...> ... <activity android:name="com.amplifyframework.auth.cognito.activities.HostedUIRedirectActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="myapp" /> </intent-filter> </activity> ...</application>
Launch Social Web UI Sign In
Sweet! You're now ready to launch sign in with your social provider's web UI.
For now, just add this method to the onCreate
method of MainActivity with whatever provider you're using (shown with Facebook below):
// Replace facebook with your chosen auth provider such as google, amazon, or appleAmplify.Auth.signInWithSocialWebUI( AuthProvider.facebook(), this, result -> Log.i("AuthQuickstart", result.toString()), error -> Log.e("AuthQuickstart", error.toString()));
// Replace facebook with your chosen auth provider such as google, amazon, or appleAmplify.Auth.signInWithSocialWebUI( AuthProvider.facebook(), this, { Log.i("AuthQuickstart", "Sign in OK: $it") }, { Log.e("AuthQuickstart", "Sign in failed", it) })
try { // Replace facebook with your chosen auth provider such as google, amazon, or apple val result = Amplify.Auth.signInWithSocialWebUI(AuthProvider.facebook(), this) Log.i("AuthQuickstart", "Sign in OK: $result")} catch (error: AuthException) { Log.e("AuthQuickstart", "Sign in failed", error)}
// Replace facebook with your chosen auth provider such as google, amazon, or appleRxAmplify.Auth.signInWithSocialWebUI(AuthProvider.facebook(), this) .subscribe( result -> Log.i("AuthQuickstart", result.toString()), error -> Log.e("AuthQuickstart", error.toString()) );