Getting started
Install Dependencies
After initialization in your project directory with amplify init
, update your App build.gradle
with the below:
//For AWSMobileClient only:implementation 'com.amazonaws:aws-android-sdk-mobile-client:ANDROID_SDK_VERSION'
//For the drop-in UI also:implementation 'com.amazonaws:aws-android-sdk-auth-userpools:ANDROID_SDK_VERSION'implementation 'com.amazonaws:aws-android-sdk-auth-ui:ANDROID_SDK_VERSION'
//For hosted UI also:implementation 'com.amazonaws:aws-android-sdk-cognitoauth:ANDROID_SDK_VERSION'
For the AWSMobileClient
alone you can have a minimum SDK version of 15, but for the drop-in UI you will need a minimum of 23 set in your build.gradle
:
minSdkVersion 15
Add the following permissions to the AndroidManifest.xml
file:
<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Build your Android Studio project.
Automated Setup
Run the following command in your project's root folder:
amplify add auth
If you have previously enabled an Amplify category that uses Auth behind the scenes, e.g. API category, you may already have an Auth configuration. In such a case, run amplify auth update
command to edit your configuration.
The CLI prompts will help you to customize your auth flow for your app. With the provided options, you can:
- Customize sign-in/registration flow
- Customize email and SMS messages for Multi-Factor Authentication
- Customize attributes for your users, e.g. name, email
- Enable 3rd party authentication providers, e.g. Facebook, Twitter, Google and Amazon
After configuring your Authentication options, update your backend:
amplify push
A configuration file called awsconfiguration.json
will be copied to your project ./app/src/main/res/raw
directory. The AWSMobileClient
will leverage this for communicating with backend services.
Lambda Triggers
The CLI allows you to configure Lambda Triggers for your Amazon Cognito User Pool. These enable you to add custom functionality to your registration and authentication flows.
Manual Setup
For manual configuration without the CLI, you must have an awsconfiguration.json
file with the following:
- Cognito User Pools:
CognitoUserPool : { Default: ...}
- Cognito Identity Pools:
IdentityManager
andCredentialsProvider: {CognitoIdentity: ...}
{ "IdentityManager": { "Default": {} }, "CredentialsProvider": { "CognitoIdentity": { "Default": { "PoolId": "XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab", "Region": "XX-XXXX-X" } } }, "CognitoUserPool": { "Default": { "PoolId": "XX-XXXX-X_abcd1234", "AppClientId": "XXXXXXXX", "Region": "XX-XXXX-X" } } }
If you are using both Cognito User Pools and Identity Pools, such as in Federated scenarios, you will need all of the keys mentioned above.
Initialization
Go to your MainActivity and inside the onCreate()
run the initialize()
routine:
AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() { @Override public void onResult(UserStateDetails userStateDetails) { Log.i("INIT", "onResult: " + userStateDetails.getUserState()); }
@Override public void onError(Exception e) { Log.e("INIT", "Initialization error.", e); } });
Build and run your program to see the initialized client in LOGCAT messages. Since you haven't logged in yet it will print a state of SIGNED_OUT
. The getUserState()
function returns an ENUM which you can perform different actions in your workflow. For example:
AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() { @Override public void onResult(UserStateDetails userStateDetails) { switch (userStateDetails.getUserState()){ case SIGNED_IN: runOnUiThread(new Runnable() { @Override public void run() { TextView textView = (TextView) findViewById(R.id.text); textView.setText("Logged IN"); } }); break; case SIGNED_OUT: runOnUiThread(new Runnable() { @Override public void run() { TextView textView = (TextView) findViewById(R.id.text); textView.setText("Logged OUT"); } }); break; default: AWSMobileClient.getInstance().signOut(); break; } }
@Override public void onError(Exception e) { Log.e("INIT", e.toString()); }});
You might leverage the above workflow to perform other actions in the SIGNED_IN
case, such as calling GraphQL or REST APIs with AWS AppSync or Amazon API Gateway or uploading content with Amazon S3.