Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 29, 2024

Add social provider sign-in

Amplify Android v1 is deprecated as of June 1st, 2024. No new features or bug fixes will be added. Dependencies may become outdated and potentially introduce compatibility issues.

Please use the latest version (v2) of Amplify Library for Android to get started. Refer to the upgrade guide for instructions on upgrading your application to the latest version.

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.

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.

  1. Go to Google developer console.

  2. Click Select a project Select a project button on the nav bar is circled.

  3. Click NEW PROJECT The new project button is circled on the select a project popup.

  4. Type in project name and click CREATE The create button is circled in the new project page.

  5. Once the project is created, from the left Navigation menu, select APIs & Services, then select Credentials The top left menu icon is selected, then the APIs and services option, then the credentials option.

  6. Click CONFIGURE CONSENT SCREEN The configure consent screen button is circled in the oauth consent screen section.

  7. Click CREATE The create button is circled in the OAuth consent screen section.

  8. Type in App Information and Developer contact information which are required field and click SAVE AND CONTINUE three times (OAuth consent screen -> Scopes -> Test Users) to finish setting up consent screen

  9. Back to Credentials tab, Create your OAuth2.0 credentials by choosing OAuth client ID from the Create credentials drop-down list. The Create credentials button is circled, then the oauth client ID button is circled in the credentials section..

  10. Choose Web application as Application type and name your OAuth Client.

  11. Click Create.

  12. Take note of Your client ID and Your Client Secret. You will need them for the next section in the CLI flow.

  13. Choose OK.

  1. Create a developer account with Amazon.
  2. Sign in with your Amazon credentials.
  3. You need to create an Amazon security profile to receive the Amazon client ID and client secret. Choose Create a Security Profile. The login with Amazon console with a create a new security profile button displayed.
  4. Type in a Security Profile Name, a Security Profile Description, and a Consent Privacy Notice URL. Security profile management page with steps to fill out a form for the new security profile.
  5. Choose Save.
  6. Choose Client ID and Client Secret to show the client ID and secret. You will need them for the next section in the CLI flow. Choosing client id and client secret.
  1. Sign In with your Apple developer credentials.
  2. On the main developer portal page, select Certificates, IDs, & Profiles.
  3. On the left navigation bar, select Identifier.
  4. On the Identifiers page, select the + icon.
  5. On the Register a New Identifier page, select App IDs.
  6. On the Register an App ID page, under App ID Prefix, take note of the Team ID value.
  7. Provide a description in the Description text box and provide the bundleID of the iOS app. Register an App ID in the certificates, identifiers and profiles section.
  8. Under Capabilities, select Sign in with Apple.
  9. Select Continue, review the configuration, and then select Register.
  10. On the Identifiers page, on the right, select App IDs, and then select Services ID.
  11. Select the + icon and, on the Register a New Identifier page, select Services IDs.
  12. Provide a description in the Description text box and provide an identifier for the service id. Register a services ID in the certificates, identifiers and profiles section.
  13. Continue and register the service id.

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

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>`

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.
  1. Go to the Google developer console.

  2. On the left navigation bar, look for APIs & Services under Pinned or under More Products if not pinned.

  3. Within the APIs and Services sub menu, choose Credentials.

  4. Select the client you created in the first step and click the edit button.

  5. Type your user pool domain into Authorized Javascript origins.

  6. Type your user pool domain with the /oauth2/idpresponse endpoint into Authorized Redirect URIs.

    The URLs 1 form fields for authorized Javascript origins and authorized redirect URLs are circled.

    Note: If you saw an error message Invalid Redirect: domain must be added to the authorized domains list before submitting. when adding the endpoint, please go to the authorized domains list and add the domain.

  7. Click Save.

  1. Sign in with your Amazon credentials.
  2. Hover over the gear and choose Web Settings associated with the security profile you created in the previous step, and then choose Edit. The web settings option is selected in the dropdown menu from the gear icon.
  3. Type your user pool domain into Allowed Origins and type your user pool domain with the /oauth2/idpresponse endpoint into Allowed Return URLs. Userpool domain is typed into the allowed origins field with /oauth2/ as the endpoint in the Allowed Return URLs field.
  4. Choose Save.
  1. Sign In with your Apple developer credentials.
  2. On the main developer portal page, select Certificates, IDs, & Profiles.
  3. On the left navigation bar, select Identifiers and then select Service IDs from the drop down list on the right.
  4. Select the service id created in Setup your auth provider step above.
  5. Enabled Sign In with Apple and select Configure.
  6. Under Primary App ID select the app id that was created before.
  7. Type your user pool domain into Domains and Subdomains.
  8. Type your user pool domain with the /oauth2/idpresponse endpoint into Return URLs. The return URLs text field is selected.
  9. Click Next, review the information, then select Done.
  10. On Edit your Services ID Configuration click Continue, review the information, then select Save.
  11. On the main Certificates, Identifiers & Profiles, select Keys.
  12. On the Keys page, select the + icon.
  13. Provide a name for the key under Key Name.
  14. Enable Sign in with Apple and select Configure The sign in with apple option is enabled and the key name text field is filled out.
  15. Under Primary App ID select the app id that was created before.
  16. Click on Save
  17. On Register a New Key click Continue, review the information, then select Register.
  18. On the page you are redirected to take note of the Key ID and download the .p8 file containing the private key. The download key page is shown with the option to download the .p8 file with the private key.

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>

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:

<activity
android:name="com.amazonaws.mobileconnectors.cognitoauth.activities.CustomTabsRedirectActivity"
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>

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:

@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");
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
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")
}
}

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

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())
);
Amplify.Auth.signInWithSocialWebUI(AuthProvider.facebook(), this,
{ Log.i("AuthQuickstart", "Sign in OK: $it") },
{ Log.e("AuthQuickstart", "Sign in failed", it) }
)
try {
val result = Amplify.Auth.signInWithSocialWebUI(AuthProvider.facebook(), this)
Log.i("AuthQuickstart", "Sign in OK: $result")
} catch (error: AuthException) {
Log.e("AuthQuickstart", "Sign in failed", error)
}
RxAmplify.Auth.signInWithSocialWebUI(AuthProvider.facebook(), this)
.subscribe(
result -> Log.i("AuthQuickstart", result.toString()),
error -> Log.e("AuthQuickstart", error.toString())
);