3

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.

Josh Field
  • 63
  • 8

1 Answers1

1

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

Brian Winant
  • 2,915
  • 15
  • 17
  • 1
    Thanks, is there an example of this somewhere? Also I read that the aws SDK does this for you and is cached in the SharePreferences, but I'm not sure how to access this. – Josh Field Jul 27 '18 at 17:24
  • Cognito library does not allow you to "just refresh session". This logic is internal - there is nothing to put refresh token in once you take it out of CognitoUser. Please provide the method name, if you know about one. – Agent_L Oct 23 '20 at 16:24
  • See https://github.com/aws-amplify/amplify-js/tree/master/packages/amazon-cognito-identity-js, "Use case 32. Handling expiration of the Id Token" specifically `cognitoUser.refreshSession` – Brian Winant Oct 24 '20 at 02:22