Page updated Nov 15, 2023

Set up Amplify Auth

Amplify Android v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Android to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

The Amplify Auth category provides an interface for authenticating a user. Behind the scenes, it provides the necessary authorization to the other Amplify categories. It comes with default, built-in support for Amazon Cognito User Pool and Identity Pool. The Amplify CLI helps you create and configure the auth category with an authentication provider.

Goal

To setup and configure your application with Amplify Auth and go through a simple api to check the current auth session.

Prerequisites

  • An Android application targeting at least Android SDK API level 16 with Amplify libraries integrated

Install Amplify Libraries

Add the following dependency to your app's build.gradle along with others you added above in Prerequisites and click "Sync Now" when prompted:

dependencies { implementation 'com.amplifyframework:aws-auth-cognito:ANDROID_V1_VERSION' }
1dependencies {
2 implementation 'com.amplifyframework:aws-auth-cognito:ANDROID_V1_VERSION'
3}

Set Up Backend Resources

The most common way to use Authentication with Amplify is via the Amplify CLI, which allows you to create new Amazon Cognito resources or import existing ones. However, you can also use the Amplify Studio console to configure authentication or use the Amplify.configure() method to set up authentication with existing resources.

Prerequisites: Install and configure the Amplify CLI in addition to the Amplify libraries and necessary dependencies.

To start provisioning auth resources in the backend, go to your project directory and execute the command:

amplify add auth
1amplify add auth

Enter the following when prompted:

? Do you want to use the default authentication and security configuration? `Default configuration` ? How do you want users to be able to sign in? `Username` ? Do you want to configure advanced settings? `No, I am done.`
1? Do you want to use the default authentication and security configuration?
2 `Default configuration`
3? How do you want users to be able to sign in?
4 `Username`
5? Do you want to configure advanced settings?
6 `No, I am done.`

If you have previously enabled an Amplify category that uses Auth behind the scenes (e.g. API category), you can run the amplify update auth command to edit your configuration if needed.

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

amplify push
1amplify push

Upon completion, amplifyconfiguration.json should be updated to reference provisioned backend auth resources. Note that these files should already be a part of your project if you followed the project setup walkthrough.

Initialize Amplify Auth

Add the Auth plugin before calling Amplify.configure. Update the code you added in Prerequisites:

// Add this line, to include the Auth plugin. Amplify.addPlugin(new AWSCognitoAuthPlugin()); Amplify.configure(getApplicationContext());
1// Add this line, to include the Auth plugin.
2Amplify.addPlugin(new AWSCognitoAuthPlugin());
3Amplify.configure(getApplicationContext());

Check the current auth session

You can now check the current auth session.

For testing purposes, you can run this from your MainActivity's onCreate method.

Amplify.Auth.fetchAuthSession( result -> Log.i("AmplifyQuickstart", result.toString()), error -> Log.e("AmplifyQuickstart", error.toString()) );
1Amplify.Auth.fetchAuthSession(
2 result -> Log.i("AmplifyQuickstart", result.toString()),
3 error -> Log.e("AmplifyQuickstart", error.toString())
4);

The isSignedIn property of the authSession will be false since you haven't signed in to the category yet.

Next Steps

Congratulations! You've successfully setup AWS Cognito Auth plugin. Check out the following links to see other Amplify Auth use cases: