I cannot find information on how to refresh access tokens using AWS' Amplify Android library.
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 my database, I use the DynamoDBMapper
from the AWS SDK for Android.
So far I have tried to force refresh the tokens in the following ways:
auth.currentUser
AWSMovileClient.getInstance().tokens
AWSMobileClient.getInstance().currentUserState()
Amplify.Auth.fetchAuthSession()
None of the first three were working. fetchAuthSession(...)
is the technique I am using for now, but it is asynchronous and takes a moment to produce a result. I have to run it synchronously in my splash activity
. Otherwise, if it is async and there is a request to the database right that depends on its results, the database call will cause a NotAuthorizedException
.
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 the 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?