Page updated Nov 14, 2023

Add social provider sign-in

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.

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.

Setup Your Auth Provider

  1. Create a developer account with Facebook.

  2. Sign In with your Facebook credentials.

  3. Choose My Apps from the top navigation bar, and on the page that loads choose Create App. Create App button in the My Apps page of the Facebook developer account.

  4. For your use case, choose Set up Facebook Login. Set up Facebook Login option selected from list.

  5. For platform, choose Website and select No, I'm not building a game.

  6. Give your Facebook app a name and choose Create app. Form fields for the Facebook create app form.

  7. On the left navigation bar, choose Settings and then Basic. App ID and App Secret in the basic settings tab of the dashboard.

  8. Note the App ID and the App Secret. You will use them in the next section during the CLI flow.

Configure Auth Category

Once you have the social provider configured, run the following in your project’s root folder

amplify add auth ## "amplify update auth" if already configured
1amplify add auth ## "amplify update auth" if already configured

Choose the following options (the last steps are specific to Facebook here but are similar for other providers):

? Do you want to use the default authentication and security configuration? `Default configuration with Social Provider (Federation)` ? How do you want users to be able to sign in? `Username` ? Do you want to configure advanced settings? `No, I am done.` ? What domain name prefix do you want to use? `(default)` ? Enter your redirect signin URI: `myapp://callback/` ? Do you want to add another redirect signin URI `No` ? Enter your redirect signout URI: `myapp://signout/` ? Do you want to add another redirect signout URI `No` ? Select the social providers you want to configure for your user pool: `<choose your provider and follow the prompts to input the proper tokens>`
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 do you want to use?
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 `<choose your provider and follow the prompts to input the proper tokens>`

Run amplify push to publish your changes. Once finished, it will display an auto generated URL for your web UI. You can retrieve your user pool domain URL at anytime by running amplify status using the CLI.

You need to now inform your auth provider of this URL:

  1. Sign In to your Facebook developer account with your Facebook credentials.

  2. Choose My Apps from the top navigation bar, and on the Apps page, choose your App you created before.

  3. On the left navigation bar, choose Products. Add Facebook Login if it isn't already added.

  4. If already added, choose Settings under the Configure dropdown. The Settings option is circled from the configure dropdown.

  5. Under Valid OAuth Redirect URIs type your user pool domain with the /oauth2/idpresponse endpoint.

    https://<your-user-pool-domain>/oauth2/idpresponse

Userpool domain is pasted into the text field with /oauth2/ endpoint.

  1. Save changes.

Federated sign-in does not invoke any Custom authentication challenge Lambda triggers, Migrate user Lambda trigger, Custom message Lambda trigger, or Custom sender Lambda triggers in your user pool. For information on the supported Lambda triggers refer to the AWS documentation

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:

<queries> <intent> <action android:name="android.intent.action.VIEW" /> <data android:scheme="https" /> </intent> <intent> <action android:name= "android.support.customtabs.action.CustomTabsService" /> </intent> </queries> <application ...> ... <activity android:name="com.amplifyframework.auth.cognito.activities.HostedUIRedirectActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="myapp" /> </intent-filter> </activity> ... </application>
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>

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:

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == AWSCognitoAuthPlugin.WEB_UI_SIGN_IN_ACTIVITY_CODE && resultCode == Activity.RESULT_CANCELED) { Log.i("AuthQuickStart", "User canceled sign in"); } }
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}

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

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == AWSCognitoAuthPlugin.WEB_UI_SIGN_IN_ACTIVITY_CODE) { Amplify.Auth.handleWebUISignInResponse(data); } }
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}

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 Social Web UI Sign In

Sweet! You're now ready to launch sign in with your social provider's web UI.

For now, just add this method to the onCreate method of MainActivity with whatever provider you're using (shown with Facebook below):

Amplify.Auth.signInWithSocialWebUI(AuthProvider.facebook(), this, result -> Log.i("AuthQuickstart", result.toString()), error -> Log.e("AuthQuickstart", error.toString()) );
1Amplify.Auth.signInWithSocialWebUI(AuthProvider.facebook(), this,
2 result -> Log.i("AuthQuickstart", result.toString()),
3 error -> Log.e("AuthQuickstart", error.toString())
4);