Saturday, 14 November 2020

Refresh tokens with AWS Amplify on Android

I cannot find the answer on how to refresh access tokens using Amplify on Android.
In the documentation and on some forums it was suggested that it all happens automatically but in my case, it does not.

In the app I use Amplify.Auth for user authentication, also Amplify.Storage and Amplify.Predictions. To query database I use DynamoDBMapper.

I tried:

  • auth.currentUser to force token refresh but it seems it doesn't work
  • AWSMovileClient.getInstance().tokens uses cached tokens only
  • AWSMobileClient.getInstance().currentUserState() to refresh it manually
  • Amplify.Auth.fetchAuthSession() is the technique I use now but it is async and takes a moment to produce a result. I run it synchronously in my splash activity cuz if it is acync, if there is a request to the database right after this, it returns NotAuthorizedException.

None of the above methods worked as expected.
Also, the refresh token can be set to like 10 years, so it is not a problem at the moment, the problem is that the new access token is not being created after old one expires and I cannot do any authenticated user action.

Is there a way to quickly check if tokens are valid and if not, refresh them?



from Refresh tokens with AWS Amplify on Android

No comments:

Post a Comment