Page updated Nov 3, 2023

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

When configuring social sign-in through the Amplify CLI, it's important to exercise caution when designating attributes as "required." Different social identity providers have varied scopes in terms of the information they respond back to Cognito with. User pool attributes that are initially set up as "required" cannot be changed later, and may require you to migrate the users or create a new user pool.

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.

1AuthSignUpOptions options = AuthSignUpOptions.builder()
2 .userAttribute(AuthUserAttributeKey.email(), "my@email.com")
3 .build();
4Amplify.Auth.signUp("username", "Password123", options,
5 result -> Log.i("AuthQuickStart", "Result: " + result.toString()),
6 error -> Log.e("AuthQuickStart", "Sign up failed", error)
7);
1val options = AuthSignUpOptions.builder()
2 .userAttribute(AuthUserAttributeKey.email(), "my@email.com")
3 .build()
4Amplify.Auth.signUp("username", "Password123", options,
5 { Log.i("AuthQuickStart", "Sign up succeeded: $it") },
6 { Log.e ("AuthQuickStart", "Sign up failed", it) }
7)
1val options = AuthSignUpOptions.builder()
2 .userAttribute(AuthUserAttributeKey.email(), "my@email.com")
3 .build()
4try {
5 val result = Amplify.Auth.signUp("username", "Password123", options)
6 Log.i("AuthQuickStart", "Result: $result")
7} catch (error: AuthException) {
8 Log.e("AuthQuickStart", "Sign up failed", error)
9}
1RxAmplify.Auth.signUp(
2 "username",
3 "Password123",
4 AuthSignUpOptions.builder().userAttribute(AuthUserAttributeKey.email(), "my@email.com").build())
5 .subscribe(
6 result -> Log.i("AuthQuickStart", "Result: " + result.toString()),
7 error -> Log.e("AuthQuickStart", "Sign up failed", error)
8 );

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.

1Amplify.Auth.confirmSignUp(
2 "username",
3 "the code you received via email",
4 result -> Log.i("AuthQuickstart", result.isSignUpComplete() ? "Confirm signUp succeeded" : "Confirm sign up not complete"),
5 error -> Log.e("AuthQuickstart", error.toString())
6);
1Amplify.Auth.confirmSignUp(
2 "username", "the code you received via email",
3 { result ->
4 if (result.isSignUpComplete) {
5 Log.i("AuthQuickstart", "Confirm signUp succeeded")
6 } else {
7 Log.i("AuthQuickstart","Confirm sign up not complete")
8 }
9 },
10 { Log.e("AuthQuickstart", "Failed to confirm sign up", it) }
11)
1try {
2 val code = "code you received via email"
3 val result = Amplify.Auth.confirmSignUp("username", code)
4 if (result.isSignUpComplete) {
5 Log.i("AuthQuickstart", "Signup confirmed")
6 } else {
7 Log.i("AuthQuickstart", "Signup confirmation not yet complete")
8 }
9} catch (error: AuthException) {
10 Log.e("AuthQuickstart", "Failed to confirm signup", error)
11}
1RxAmplify.Auth.confirmSignUp("username", "the code you received via email")
2 .subscribe(
3 result -> Log.i("AuthQuickstart", result.isSignUpComplete() ? "Confirm signUp succeeded" : "Confirm sign up not complete"),
4 error -> Log.e("AuthQuickstart", error.toString())
5 );

You will know the sign up flow is complete if you see the following in your console window:

1Confirm 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:

1Amplify.Auth.signIn(
2 "username",
3 "password",
4 result -> Log.i("AuthQuickstart", result.isSignedIn() ? "Sign in succeeded" : "Sign in not complete"),
5 error -> Log.e("AuthQuickstart", error.toString())
6);
1Amplify.Auth.signIn("username", "password",
2 { result ->
3 if (result.isSignedIn) {
4 Log.i("AuthQuickstart", "Sign in succeeded")
5 } else {
6 Log.i("AuthQuickstart", "Sign in not complete")
7 }
8 },
9 { Log.e("AuthQuickstart", "Failed to sign in", it) }
10)
1try {
2 val result = Amplify.Auth.signIn("username", "password")
3 if (result.isSignedIn) {
4 Log.i("AuthQuickstart", "Sign in succeeded")
5 } else {
6 Log.e("AuthQuickstart", "Sign in not complete")
7 }
8} catch (error: AuthException) {
9 Log.e("AuthQuickstart", "Sign in failed", error)
10}
1RxAmplify.Auth.signIn("username", "password")
2 .subscribe(
3 result -> Log.i("AuthQuickstart", result.isSignedIn() ? "Sign in succeeded" : "Sign in not complete"),
4 error -> Log.e("AuthQuickstart", error.toString())
5 );

You will know the sign in flow is complete if you see the following in your console window:

1Sign 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

Note: If you create or update an SMS MFA configuration for your Cognito user pool, the Cognito service will send a test SMS message to an internal number in order to verify your configuration. You will be charged for these test messages by Amazon SNS.

For information about Amazon SNS pricing, see Worldwide SMS Pricing.

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:

1? Do you want to use the default authentication and security configuration?
2 `Manual configuration`
3? Select the authentication/authorization services that you want to use:
4 `User Sign-Up, Sign-In, connected with AWS IAM controls (Enables per-user Storage features for images or other content, Analytics, and more)`
5? Please provide a friendly name for your resource that will be used to label this category in the project:
6 `<default>`
7? Please enter a name for your identity pool.
8 `<default>`
9? Allow unauthenticated logins? (Provides scoped down permissions that you can control via AWS IAM)
10 `Yes`
11? Do you want to enable 3rd party authentication providers in your identity pool?
12 `No`
13? Please provide a name for your user pool:
14 `<default>`
15Warning: you will not be able to edit these selections.
16? How do you want users to be able to sign in?
17 `Username`
18? Do you want to add User Pool Groups?
19 `No`
20? Do you want to add an admin queries API?
21 `No`
22? Multifactor authentication (MFA) user login options:
23 `ON (Required for all logins, can not be enabled later)`
24? For user login, select the MFA types:
25 `SMS Text Message`
26? Please specify an SMS authentication message:
27 `Your authentication code is {####}`
28? Email based user registration/forgot password:
29 `Enabled (Requires per-user email entry at registration)`
30? Please specify an email verification subject:
31 `Your verification code`
32? Please specify an email verification message:
33 `Your verification code is {####}`
34? Do you want to override the default password policy for this User Pool?
35 `No`
36Warning: you will not be able to edit these selections.
37? What attributes are required for signing up?
38 `Email, Phone Number (This attribute is not supported by Facebook, Login With Amazon.)`
39? Specify the app's refresh token expiration period (in days):
40 `30`
41? Do you want to specify the user attributes this app can read and write?
42 `No`
43? Do you want to enable any of the following capabilities?
44 `NA`
45? Do you want to use an OAuth flow?
46 `No`
47? Do you want to configure Lambda Triggers for Cognito?
48 `No`

To push your changes to the cloud, execute the command:

1amplify 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:

1ArrayList<AuthUserAttribute> attributes = new ArrayList<>();
2attributes.add(new AuthUserAttribute(AuthUserAttributeKey.email(), "my@email.com"));
3attributes.add(new AuthUserAttribute(AuthUserAttributeKey.phoneNumber(), "+15551234567"));
4
5Amplify.Auth.signUp(
6 "username",
7 "Password123",
8 AuthSignUpOptions.builder().userAttributes(attributes).build(),
9 result -> Log.i("AuthQuickstart", result.toString()),
10 error -> Log.e("AuthQuickstart", error.toString())
11);
1val attrs = mapOf(
2 AuthUserAttributeKey.email() to "my@email.com",
3 AuthUserAttributeKey.phoneNumber() to "+15551234567"
4)
5val options = AuthSignUpOptions.builder()
6 .userAttributes(attrs.map { AuthUserAttribute(it.key, it.value) })
7 .build()
8Amplify.Auth.signUp("username", "Password123", options,
9 { Log.i("AuthQuickstart", "Sign up result = $it") },
10 { Log.e("AuthQuickstart", "Sign up failed", it) }
11)
1val attrs = mapOf(
2 AuthUserAttributeKey.email() to "my@email.com",
3 AuthUserAttributeKey.phoneNumber() to "+15551234567"
4)
5val options = AuthSignUpOptions.builder()
6 .userAttributes(attrs.map { AuthUserAttribute(it.key, it.value) })
7 .build()
8try {
9 val result = Amplify.Auth.signUp("username", "Password123", options)
10 Log.i("AuthQuickstart", "Sign up OK: $result")
11} catch (error: AuthException) {
12 Log.e("AuthQuickstart", "Sign up failed", error)
13}
1ArrayList<AuthUserAttribute> attributes = new ArrayList<>();
2attributes.add(new AuthUserAttribute(AuthUserAttributeKey.email(), "my@email.com"));
3attributes.add(new AuthUserAttribute(AuthUserAttributeKey.phoneNumber(), "+15551234567"));
4
5RxAmplify.Auth.signUp(
6 "username",
7 "Password123",
8 AuthSignUpOptions.builder().userAttributes(attributes).build())
9 .subscribe(
10 result -> Log.i("AuthQuickstart", result.toString()),
11 error -> Log.e("AuthQuickstart", error.toString())
12 );

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:

Note that you must call confirmSignIn in the same app session as you call signIn. If you close the app, you'll need to call signIn again. As a result, for testing purposes, you'll at least need an input field where you can enter the code sent via SMS and feed it to confirmSignIn.

1Amplify.Auth.confirmSignIn(
2 "confirmation code received via SMS",
3 result -> Log.i("AuthQuickstart", result.toString()),
4 error -> Log.e("AuthQuickstart", error.toString())
5);
1Amplify.Auth.confirmSignIn("code received via SMS",
2 { Log.i("AuthQuickstart", "Confirmed signin: $it") },
3 { Log.e("AuthQuickstart", "Failed to confirm signin", it) }
4)
1try {
2 val result = Amplify.Auth.confirmSignIn("code received via SMS")
3 Log.i("AuthQuickstart", "Confirmed signin: $result")
4} catch (error: AuthException) {
5 Log.e("AuthQuickstart", "Failed to confirm signin", error)
6}
1RxAmplify.Auth.confirmSignIn("confirmation code received via SMS")
2 .subscribe(
3 result -> Log.i("AuthQuickstart", result.toString()),
4 error -> Log.e("AuthQuickstart", error.toString())
5 );