Skip to content

Instantly share code, notes, and snippets.

@TrekSoft
Last active May 19, 2020 20:34
Show Gist options
  • Save TrekSoft/33190858041e6e0810d1324735bb0666 to your computer and use it in GitHub Desktop.
Save TrekSoft/33190858041e6e0810d1324735bb0666 to your computer and use it in GitHub Desktop.
Auth Web UI Documentation v0

Auth Web UI Sign In Documentation

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())
);
@sebsto
Copy link

sebsto commented May 19, 2020

  1. the whole content, really ? it has also Version, UserAgent, Appsync attributes.

@TrekSoft
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment