Bugz
Bugz

Reputation: 118

Simple Auth with Cognito AWS via Android

I've managed to get Cognito to work on our web React app but now I want to get it working on our Android app. However, it seems that the Auth process works different on Android than via the web-app. Here's the code :

Our React web app code looks like:

Amplify.configure({
  Auth: {
    mandatorySignId: true,
    region: config.cognito.REGION,
    userPoolId: config.cognito.USER_POOL_ID,
    userPoolWebClientId: config.cognito.APP_CLIENT_ID
  }
});

const user = await Auth.signIn(username, password);

How do I do the same on Android Studio? I've looked at some examples like this

        Auth.Builder builder = new Auth.Builder();
        builder.setAppClientId(appClientId)
                .setUserPoolId(userPoolId)
                .setAppCognitoWebDomain("")//What is this???
                .setApplicationContext(this.getContext())
                .setAuthHandler(new callback())
                .setSignInRedirect("")//Why is this here?
                .setSignOutRedirect("");//Why is this here?
        this.auth = builder.build();

This Auth builder would open a browser and manage the login there then go back to the app as far as I can see. What if I wanted to use my own UI to get the username and password like in the web-app example?

Does anyone have any sample code on how to do this?

Thanks for the help! It's really appreciated

Upvotes: 1

Views: 525

Answers (1)

Gail
Gail

Reputation: 316

It appears the only hook provided in this CognitoAuth (com.amazonaws:aws-android-sdk-cognitoauth:2.16.12@aar) Library is a bundle property, via .setCustomTabsExtras(bundle), that can be assigned to an app-created Bundle instance and populated with properties that you will find in the Chrome Custom Tabs library, https://github.com/GoogleChrome/custom-tabs-client

The CognitoAuth Library's source code shows, in AuthClient.java, that it uses the Chrome Custom Tabs library as part of it's hosted UI, specifically CustomTabsIntent among other classes, and it will accept a bundle instance containing key/value pairs used by the mobile browser for customization. These customizations are described in Chrome Custom Tabs documentation and source code.

I've been working with it myself and, so far, I haven't been able to get much customization to work, but then again, I may not have landed on the right key/value pairs to put into the bundle. I had to time-box my effort and simply ran out of time but I did get the Toolbar background color to change so there's hope.

Here is what I've used:

Bundle customTabs = new Bundle();
customTabs.putInt(CustomTabsIntent.EXTRA_TOOLBAR_COLOR, R.color.white);

I had to include the Chrome Custom Tabs library in my app to access the constants used in the Bundle. Note: the toolbar color change is not correct as the Chrome mobile browser appears to be mapping the new color my app provides to it's own values. Not surprised, but I still don't know how to fix it or if I'm just not putting the right value in there.

Upvotes: 1

Related Questions