Josh Field
Josh Field

Reputation: 63

AWS Cognito: How to keep an Android user logged in?

Currently each time the app is restarted the user has to log in. I would like the app to remember the user until they manually sign out. Below is what I thought would work, but its just bypassing the login activity completely.

@Override
protected void onStart() {
    super.onStart();
    if(userPool.getCurrentUser() != null){
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        finish();
        startActivity(intent);
    }
}

So, userPool.getcurrentUser() must not be null in the beginning, even though I don't create a CognitoUser object until after the login button is clicked.

Thanks for your help.

Upvotes: 3

Views: 1820

Answers (1)

Brian Winant
Brian Winant

Reputation: 3035

Setup your user pool client so that the refresh token has the max expiration. On first login, save the refresh token. Then every time the app is restarted use the refresh token to refresh the current user session and get new id/access tokens

Upvotes: 1

Related Questions