Skip to content

Android Authentication

Sharina Stubbs edited this page Nov 5, 2019 · 4 revisions

Resources:

Docs

Good clear docs on Android

  • "drop-in" UI available to create a packaged deal for user authentication

Steps

Install dependencies

Add dependencies to build.gradle Update minSdkVersion to 23, found within defaultConfig

in terminal: amplify add auth, then amplify status, then amplify status`

Make sure manifest contains internet and network only stuff

Initialization

Go to MainActivity

Checks if user is logged in, and initializes aws mobile client:

Use this starting code, but see class example on Nov 4 to adapt it:

AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {

        @Override
        public void onResult(UserStateDetails userStateDetails) {
            Log.i("INIT", "onResult: " + userStateDetails.getUserState());
        }

        @Override
        public void onError(Exception e) {
            Log.e("INIT", "Initialization error.", e);
        }
    }
);

How to SignIn

In MainActivity view, add a signin button

Go to MainActivity and grab id for button

Button signinButton = findViewById(R.id.signin);

Create an onClickListener and check docs for signIn starting code:

  • See Nov 4 class code for adaptations that may work for taskmaster
  • Note that when you type in new Call in the first line, it will predict which one you need, and will autofill a bunch of stuff (the Overrides)
  • Cognito user group deals with the usernames etc.
    • Go to aws console and look for user pools
// 'this' refers the the current active activity
AWSMobileClient.getInstance().showSignIn(this, new Callback<UserStateDetails>() {
    @Override
    public void onResult(UserStateDetails result) {
        Log.d(TAG, "onResult: " + result.getUserState());
    }

    @Override
    public void onError(Exception e) {
        Log.e(TAG, "onError: ", e);
    }
});

How to signOut

In MainActivity, add a signout button in the view to use to call the code needed.

Grab the button id and set an onClickListener in the MainActivity in create. Use starting code:

AWSMobileClient.getInstance().signOut();

Can say goodbye to the user upon signout by adding the following code before the .signOut:

public void onClick(View event) {
  TextView helloView = findViewById(R.id.helloTextView);
  String username = AWSMobileClient.getIstnace().getUsername();
  helloView.setText("bye " + username!);

  AWSMobileClient.getInstance().signout();

To take the user out of the app, redirect back to home (MainActivity.finish will quit the whole app!)

Customize the signin UI

Starting code from docs. Check example in BuyCheapThings in class demo for how to integrate it into code.

 SignInUIOptions.builder()
                .nextActivity(NextActivity.class)
                .build(),

To get an image, put a jpg or something into res > drawables, then reference wit with R.drawable.jpg-here

To trigger a login immediately, without going to the main page:

Put the signin code into the onCreate initialization

AWSMObileClient.getInstance().initialize(getApplicationContext()..
  @Override
  public void onResult(UserSTateDetails result) {
   if(result.getUserState().toString().equals("SIGNED_OUT)) {

To get information about the user...

In the onResult code, within the onClickListener, can check different things about the user

public void onResult(UserStateDetails result) {
  Log.i(AWSMobileClient.getInstance().getUsername());

Can now use the .getUsername to update the Hello that is in the MainActivity view.

Within MainActicity, onResume, we can now say our...

String username = AWSMobileClient.getInstance().getUsername();
Clone this wiki locally