Enable sign-in
The Auth category can be used to register a user, confirm attributes like email/phone, and sign in with optional multi-factor authentication. It is set up to use Amazon Cognito User Pools which manages the users and their properties.
Prerequisites
- An app setup according to the getting started walkthrough
Register a user
The default CLI flow as mentioned in the getting started guide requires a username, password and a valid email id as parameters to register a user. Invoke the following api to initiate a sign up flow.
AuthSignUpOptions options = AuthSignUpOptions.builder() .userAttribute(AuthUserAttributeKey.email(), "my@email.com") .build();Amplify.Auth.signUp("username", "Password123", options, result -> Log.i("AuthQuickStart", "Result: " + result.toString()), error -> Log.e("AuthQuickStart", "Sign up failed", error));
val options = AuthSignUpOptions.builder() .userAttribute(AuthUserAttributeKey.email(), "my@email.com") .build()Amplify.Auth.signUp("username", "Password123", options, { Log.i("AuthQuickStart", "Sign up succeeded: $it") }, { Log.e ("AuthQuickStart", "Sign up failed", it) })
val options = AuthSignUpOptions.builder() .userAttribute(AuthUserAttributeKey.email(), "my@email.com") .build()try { val result = Amplify.Auth.signUp("username", "Password123", options) Log.i("AuthQuickStart", "Result: $result") } catch (error: AuthException) { Log.e("AuthQuickStart", "Sign up failed", error)}
RxAmplify.Auth.signUp( "username", "Password123", AuthSignUpOptions.builder().userAttribute(AuthUserAttributeKey.email(), "my@email.com").build()) .subscribe( result -> Log.i("AuthQuickStart", "Result: " + result.toString()), error -> Log.e("AuthQuickStart", "Sign up failed", error) );
The next step in the sign up flow is to confirm the user. A confirmation code will be sent to the email id provided during sign up. Enter the confirmation code received via email in the confirmSignUp
call.
Amplify.Auth.confirmSignUp( "username", "the code you received via email", result -> Log.i("AuthQuickstart", result.isSignUpComplete() ? "Confirm signUp succeeded" : "Confirm sign up not complete"), error -> Log.e("AuthQuickstart", error.toString()));
Amplify.Auth.confirmSignUp( "username", "the code you received via email", { result -> if (result.isSignUpComplete) { Log.i("AuthQuickstart", "Confirm signUp succeeded") } else { Log.i("AuthQuickstart","Confirm sign up not complete") } }, { Log.e("AuthQuickstart", "Failed to confirm sign up", it) })
try { val code = "code you received via email" val result = Amplify.Auth.confirmSignUp("username", code) if (result.isSignUpComplete) { Log.i("AuthQuickstart", "Signup confirmed") } else { Log.i("AuthQuickstart", "Signup confirmation not yet complete") }} catch (error: AuthException) { Log.e("AuthQuickstart", "Failed to confirm signup", error)}
RxAmplify.Auth.confirmSignUp("username", "the code you received via email") .subscribe( result -> Log.i("AuthQuickstart", result.isSignUpComplete() ? "Confirm signUp succeeded" : "Confirm sign up not complete"), error -> Log.e("AuthQuickstart", error.toString()) );
You will know the sign up flow is complete if you see the following in your console window:
Confirm signUp succeeded
Sign in a user
Implement a UI to get the username and password from the user. After the user enters the username and password you can start the sign in flow by calling the following method:
Amplify.Auth.signIn( "username", "password", result -> Log.i("AuthQuickstart", result.isSignInComplete() ? "Sign in succeeded" : "Sign in not complete"), error -> Log.e("AuthQuickstart", error.toString()));
Amplify.Auth.signIn("username", "password", { result -> if (result.isSignInComplete) { 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.isSignInComplete) { 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.isSignInComplete() ? "Sign in succeeded" : "Sign in not complete"), error -> Log.e("AuthQuickstart", error.toString()) );
You will know the sign in flow is complete if you see the following in your console window:
Sign in succeeded
You have now successfully registered a user and authenticated with that user's username and password with Amplify. The Authentication category supports other mechanisms for authentication such as web UI based sign in, sign in using other providers etc that you can explore in the other sections.
Multi-factor authentication
Some steps in setting up multi-factor authentication can only be chosen during the initial setup of Auth. If you have already added Auth via the CLI, navigate to your project directory in Terminal, run amplify auth remove
and when that completes, amplify push
to remove it.
Now, run amplify add auth
and setup Auth with the following options:
? Do you want to use the default authentication and security configuration? `Manual configuration`? Select the authentication/authorization services that you want to use: `User Sign-Up, Sign-In, connected with AWS IAM controls (Enables per-user Storage features for images or other content, Analytics, and more)`? Please provide a friendly name for your resource that will be used to label this category in the project: `<default>`? Please enter a name for your identity pool. `<default>`? Allow unauthenticated logins? (Provides scoped down permissions that you can control via AWS IAM) `Yes`? Do you want to enable 3rd party authentication providers in your identity pool? `No`? Please provide a name for your user pool: `<default>`Warning: you will not be able to edit these selections.? How do you want users to be able to sign in? `Username`? Do you want to add User Pool Groups? `No`? Do you want to add an admin queries API? `No`? Multifactor authentication (MFA) user login options: `ON (Required for all logins, can not be enabled later)`? For user login, select the MFA types: `SMS Text Message`? Please specify an SMS authentication message: `Your authentication code is {####}`? Email based user registration/forgot password: `Enabled (Requires per-user email entry at registration)`? Please specify an email verification subject: `Your verification code`? Please specify an email verification message: `Your verification code is {####}`? Do you want to override the default password policy for this User Pool? `No`Warning: you will not be able to edit these selections.? What attributes are required for signing up? `Email, Phone Number (This attribute is not supported by Facebook, Login With Amazon.)`? Specify the app's refresh token expiration period (in days): `30`? Do you want to specify the user attributes this app can read and write? `No`? Do you want to enable any of the following capabilities? `NA`? Do you want to use an OAuth flow? `No`? Do you want to configure Lambda Triggers for Cognito? `No`
To push your changes to the cloud, execute the command:
amplify push
In order to send SMS authentication codes, you must request an origination number. Authentication codes will be sent from the origination number. If your AWS account is in the SMS sandbox, you must also add a destination phone number, which can be done by going to the Amazon Pinpoint Console, selecting SMS and voice in the navigation pane, and selecting Add phone number in the Destination phone numbers tab. To check if your AWS account is in the SMS sandbox, go to the SNS console, select the Text messaging (SMS) tab from the navigation pane, and check the status under the Account information section.
When you sign up, be sure to include both email and phone attributes with the phone number formatted as follows:
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'll then confirm signup, sign in, and get back 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()) );