Page updated Nov 8, 2023

Enable sign-in with web UI

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.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for Android, you can access the documentation here.

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.

Configure Auth Category

This library's Cognito plugin currently supports the Authorization Code Grant OAuth Flow.

In terminal, navigate to your project, run amplify add auth (or if you've already added it, run amplify update auth), and choose the following options:

1? Do you want to use the default authentication and security configuration?
2 `Default configuration with Social Provider (Federation)`
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.`
7? What domain name prefix you want us to create for you?
8 `(default)`
9? Enter your redirect signin URI:
10 `myapp://callback/`
11? Do you want to add another redirect signin URI
12 `No`
13? Enter your redirect signout URI:
14 `myapp://signout/`
15? Do you want to add another redirect signout URI
16 `No`
17? Select the social providers you want to configure for your user pool:
18 `<hit enter>`

Once finished, run amplify push to publish your changes.

Update AndroidManifest.xml

Add the following activity and queries tag to your app's AndroidManifest.xml file, replacing myapp with your redirect URI prefix if necessary:

1<queries>
2 <intent>
3 <action android:name="android.intent.action.VIEW" />
4 <data android:scheme="https" />
5 </intent>
6 <intent>
7 <action android:name=
8 "android.support.customtabs.action.CustomTabsService" />
9 </intent>
10</queries>
11<application ...>
12 ...
13 <activity
14 android:name="com.amplifyframework.auth.cognito.activities.HostedUIRedirectActivity"
15 android:exported="true">
16 <intent-filter>
17 <action android:name="android.intent.action.VIEW" />
18 <category android:name="android.intent.category.DEFAULT" />
19 <category android:name="android.intent.category.BROWSABLE" />
20 <data android:scheme="myapp" />
21 </intent-filter>
22 </activity>
23 ...
24</application>

Note: These versions have known issues with sign-out after signing in via web UI. Please update to the latest version and follow the updated instructions for best results.

Add the following activity to your app's AndroidManifest.xml file, replacing myapp with whatever value you used for your redirect URI prefix:

1<activity
2 android:name="com.amazonaws.mobileconnectors.cognitoauth.activities.CustomTabsRedirectActivity"
3 android:exported="true">
4 <intent-filter>
5 <action android:name="android.intent.action.VIEW" />
6 <category android:name="android.intent.category.DEFAULT" />
7 <category android:name="android.intent.category.BROWSABLE" />
8 <data android:scheme="myapp" />
9 </intent-filter>
10</activity>

These instructions have been updated since version 1.2.0. If you set this up for a version of Amplify prior to 1.2.0, be sure to remove the intent-filter with android:scheme from your own activity as well as the singleInstance launch mode.

Add Response Handler

If you are using a version of Amplify 1.17.8 or above and have already declared HostedUIRedirectActivity in your manifest file, you only need to add the result handler if you need to capture sign in cancellations that occurred before the user submitted credentials.

Add the following result handler to whichever Activity you are calling HostedUI from:

1@Override
2protected void onActivityResult(int requestCode, int resultCode, Intent data) {
3 super.onActivityResult(requestCode, resultCode, data);
4
5 if (requestCode == AWSCognitoAuthPlugin.WEB_UI_SIGN_IN_ACTIVITY_CODE &&
6 resultCode == Activity.RESULT_CANCELED) {
7 Log.i("AuthQuickStart", "User canceled sign in");
8 }
9}
1override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
2 super.onActivityResult(requestCode, resultCode, data)
3
4 if (requestCode == AWSCognitoAuthPlugin.WEB_UI_SIGN_IN_ACTIVITY_CODE &&
5 resultCode == Activity.RESULT_CANCELED) {
6 Log.i("AuthQuickStart", "User canceled sign in")
7 }
8}

If you are using a version of Amplify below 1.17.8, you must add the following sign in result handler.

1@Override
2protected void onActivityResult(int requestCode, int resultCode, Intent data) {
3 super.onActivityResult(requestCode, resultCode, data);
4
5 if (requestCode == AWSCognitoAuthPlugin.WEB_UI_SIGN_IN_ACTIVITY_CODE) {
6 Amplify.Auth.handleWebUISignInResponse(data);
7 }
8}
1override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
2 super.onActivityResult(requestCode, resultCode, data)
3
4 if (requestCode == AWSCognitoAuthPlugin.WEB_UI_SIGN_IN_ACTIVITY_CODE) {
5 Amplify.Auth.handleWebUISignInResponse(data)
6 }
7}
1override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
2 super.onActivityResult(requestCode, resultCode, data)
3
4 if (requestCode == AWSCognitoAuthPlugin.WEB_UI_SIGN_IN_ACTIVITY_CODE) {
5 Amplify.Auth.handleWebUISignInResponse(data)
6 }
7}
1@Override
2protected void onActivityResult(int requestCode, int resultCode, Intent data) {
3 super.onActivityResult(requestCode, resultCode, data);
4
5 if (requestCode == AWSCognitoAuthPlugin.WEB_UI_SIGN_IN_ACTIVITY_CODE) {
6 RxAmplify.Auth.handleWebUISignInResponse(data);
7 }
8}

If you set this up for a version of Amplify prior to 1.2.0, be sure to remove the onNewIntent method code from your Activity that was previously specified.

Launch Web UI Sign In

Sweet! You're now ready to launch sign in with web UI. For now, just add this method to the onCreate method of MainActivity:

1Amplify.Auth.signInWithWebUI(
2 this,
3 result -> Log.i("AuthQuickStart", result.toString()),
4 error -> Log.e("AuthQuickStart", error.toString())
5);
1Amplify.Auth.signInWithWebUI(this,
2 { Log.i("AuthQuickStart", "Signin OK = $it") },
3 { Log.e("AuthQuickStart", "Signin failed", it) }
4)
1try {
2 val result = Amplify.Auth.signInWithWebUI(this)
3 Log.i("AuthQuickStart", "Signin OK: $result")
4} catch (error: AuthException) {
5 Log.e("AuthQuickStart", "Signin failed", error)
6}
1RxAmplify.Auth.signInWithWebUI(this)
2 .subscribe(
3 result -> Log.i("AuthQuickStart", result.toString()),
4 error -> Log.e("AuthQuickStart", error.toString())
5 );