Integrate AWS Amplify Cognito in Android

Posted By : Mohd Anas | 14-Dec-2020

In this blog, I am briefly describing to you how to integrate the Amplify Cognito User pool in android for User Management. 

 

To use Amplify Framework, We need to add some configurations in our system. these are the steps to add the Amplify framework in android.

 

Install Node Js or NPM

 

To install the Node Js, Click the below link and select the Operating System Whatever you are using to Download Node Js.

 

https://nodejs.org/en/download/

 

Install CLI

 

First, we need to install Amplify CLI in our system. To install the CLI we need to run the above command in the Command prompt in System Root Directory.

 

npm install -g @aws-amplify/cli

 

Note: While setting up the Amplify you need to have an AWS Developers Console Account or you can create it during setup.

 

Setup Project

 

Create a new project or you can also use an existing one. 

 

Add the line mavenCentral() within the repositories block in the build script and allprojects blocks

 

In the build.gradle app file adds the following dependencies and Java compile options to Java8.

 

android {
  // Add these lines in `android` 
compileOptions { 
sourceCompatibility JavaVersion.VERSION_1_8 
targetCompatibility JavaVersion.VERSION_1_8 
}
}
 
dependencies {
// Add line in `dependencies` 
implementation 'com.amplifyframework:core:1.1.1'
}

 

Also Read: How To Deal With The AWS Abuse Report

 

 

then run the following command in the project root directory to initialize the amplify in the project then do the basic setup.

 

amplify init

 

After doing the setup, Add the following dependency in the Gradle file to add Authentication in the application.

 

dependencies { implementation 'com.amplifyframework:aws-auth-cognito:1.1.1' }

 

 

Then run the "amplify add auth" command to set up the Authentication with CLI.

 

After that, run the command "amplify push" to push all the changes in the backend AWS Console. This command creates the user pool on the AWS Console to save all the users.

 

Initialize Amplify Auth

 

Add the Auth plugin along with any other plugins you added per the instructions in the Prerequisites section:

 

 

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

 

Also Read: Integrating AWS Elastic Transcoder With NodeJS Application

 

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.

 

Amplify.Auth.signUp(
        "username",
        "Password123",
        AuthSignUpOptions.builder().userAttribute(AuthUserAttributeKey.email(), "[email protected]").build(),
        result -> Log.i("AuthQuickStart", "Result: " + result.toString()),
        error -> Log.e("AuthQuickStart", "Sign up failed", error)
);

 

The next step in the signup 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())
);
copy

 

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())
);
copy

 

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

 

Sign in succeeded

 

You have complete the registration and authentication of the user with the username and password in 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.

 

 

Avail Our AWS Development Services To Strengthen Your Enterprise Processes

 

We are a 360-degree cloud app development company that provides end-to-end AWS application development services for varied business needs. Our development team is experienced in using and implementing a variety of Amazon Web Services such as AWS Lambda, CloudFront, ElasticSearch, Amazon S3, Amazon EC2, Kinesis, Amazon ECS, and more. For project-related queries, reach us out at [email protected]m.

 

About Author

Author Image
Mohd Anas

Anas is working as an Android Developer in Oodles Technologies. He is hard working, Self Motivated and dedicated towards his work. He is also passionate about learning new technologies related to our fields.

Request for Proposal

Name is required

Comment is required

Sending message..