1. Setup a new Android Studio app project
2. Run amplify init
on it
3. Follow the Automated CLI Setup instructions - below is an expanded version of what options to choose when running the amplify add auth
command referred to in that documentation:
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 you want us to create for you? (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: Facebook
You've opted to allow users to authenticate via Facebook. If you haven't already, you'll need to go to https://developers.facebook.com and create an App ID.
Enter your Facebook App ID for your OAuth flow: (your app id)
Enter your Facebook App Secret for your OAuth flow: (your app secret)
4. Checkout the ddaudeli/HostedUI
branch of the Amplify Android repo and follow the steps to locally publish it
5. Add the following dependencies in your app build.gradle file:
implementation 'com.amplifyframework:core:master'
implementation 'com.amplifyframework:aws-auth-cognito:master'
6. Add the following section to the bottom of the android
block of your app build.gradle file:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
7. In your AndroidManifest.xml file, add the following property to the MainActivity activity: android:launchMode="singleInstance"
e.g.
<application>
<activity
android:name=".MainActivity"
android:launchMode="singleInstance">
</activity>
</application>
8. Add this new intent filter below the existing one in the Android Manifest:
<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>
9. Set the content of MainActivity to the following:
static final String TAG = "AuthUserAcceptance";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Amplify.addPlugin(new AWSCognitoAuthPlugin());
Amplify.configure(getApplicationContext());
Log.i(TAG, "Configured!");
} catch(AmplifyException exception) {
Log.e(TAG, "Failed to configure Amplify", exception);
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if(intent.getData() != null && "myapp".equals(intent.getData().getScheme())) {
Amplify.Auth.handleWebUISignInResponse(intent);
}
}
10. Copy the contents of awsconfiguration.json (located in app/src/main/res/raw) into amplifyconfiguration.json like so:
{
"UserAgent": "aws-amplify-cli/2.0",
"Version": "1.0",
"auth": {
"plugins": {
"awsCognitoAuthPlugin": {
<paste contents of awsconfiguration.json>
}
}
}
}
11. Add the following command to the bottom of onCreate in MainActivity to sign in using the web UI:
Amplify.Auth.signInWithWebUI(
this,
result -> Log.i(TAG, result.toString()),
error -> Log.e(TAG, error.toString())
);
12. Or to sign in directly with a third party auth provider you can do:
Amplify.Auth.signInWithSocialWebUI(
AuthProvider.facebook(),
this,
result -> Log.i(TAG, result.toString()),
error -> Log.e(TAG, error.toString())
);
Yeah, we currently copy the entire contents to ensure AWSMobileClient continues to get what it has been getting. There is definitely stuff in there that can be removed and we will be looking into cleaning that up as a next phase of improvement.