Add social provider sign-in
Prerequisites
- An app setup according to the getting started walkthrough
Setup Your Auth Provider
-
Create a developer account with Facebook.
-
Sign In with your Facebook credentials.
-
Choose My Apps from the top navigation bar, and on the page that loads choose Create App.
-
For your use case, choose Set up Facebook Login.
-
For platform, choose Website and select No, I'm not building a game.
-
Give your Facebook app a name and choose Create app.
-
On the left navigation bar, choose Settings and then Basic.
-
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
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):
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 URI12 `No`13? Enter your redirect signout URI:14 `myapp://signout/`15? Do you want to add another redirect signout URI16 `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:
-
Sign In to your Facebook developer account with your Facebook credentials.
-
Choose My Apps from the top navigation bar, and on the Apps page, choose your App you created before.
-
On the left navigation bar, choose Products. Add Facebook Login if it isn't already added.
-
If already added, choose Settings under the Configure dropdown.
-
Under Valid OAuth Redirect URIs type your user pool domain with the
/oauth2/idpresponse
endpoint.https://<your-user-pool-domain>/oauth2/idpresponse
- Save 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 <activity14 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
Add the following result handler to whichever Activity
you are calling HostedUI from:
1@Override2protected 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}
1@Override2protected 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}
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):
1Amplify.Auth.signInWithSocialWebUI(AuthProvider.facebook(), this,2 result -> Log.i("AuthQuickstart", result.toString()),3 error -> Log.e("AuthQuickstart", error.toString())4);