Android: Access token not refreshing (CredentialsManagerException)

Hello Auth0 community,

I have a problem with my access tokens not getting refreshed in the Android app.

I login the user with Lock like this:

 lock = Lock.newBuilder(Auth0(this), callback)
            .hideMainScreenTitle(true)
            .withScope("offline_access openid")
            .withUsernameStyle(UsernameStyle.EMAIL)
            .allowSignUp(false)
            .build(this)

I store the returned credentials (including the refresh token and id token) using the CredentialsManager like this:

  credentialsManager.saveCredentials(credentials)

Then I retrieve the access token using the credentialsManager.getCredentials() function, which as stated in the documentation

Retrieves the credentials from the storage and refresh them if they have already expired.
It will fail with [CredentialsManagerException] if the saved access_token or id_token is null,
or if the tokens have already expired and the refresh_token is null.

like this:

 override suspend fun getAccessToken(): String {
        val credentials = suspendCoroutine<Credentials> { continuation ->
            credentialsManager.getCredentials(object : Callback<Credentials, CredentialsManagerException> {
                override fun onSuccess(result: Credentials) {
                    // Use credentials
                    continuation.resume(result)
                }

                override fun onFailure(error: CredentialsManagerException) {
                    // No credentials were previously saved or they couldn't be refreshed
                    Timber.e(error)
                    continuation.resumeWithException(UnauthorizedException())
                }
            })
        }
        return credentials.accessToken
    }

However, once the access token has expired, i get the following error when attempting to retrieve the access token this way:

com.auth0.android.authentication.storage.CredentialsManagerException: An error occurred while trying to use the Refresh Token to renew the Credentials.
at com.auth0.android.authentication.storage.CredentialsManager$getCredentials$1.onFailure(CredentialsManager.kt:172)
at com.auth0.android.authentication.storage.CredentialsManager$getCredentials$1.onFailure(CredentialsManager.kt:137)
at com.auth0.android.request.internal.BaseRequest.start$lambda-2$lambda-1(BaseRequest.kt:76)
at com.auth0.android.request.internal.BaseRequest.$r8$lambda$Pfq2vyLDFhY2XM__E5Uh2vtQFxk(Unknown Source:0)
at com.auth0.android.request.internal.BaseRequest$$ExternalSyntheticLambda0.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7838)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: com.auth0.android.authentication.AuthenticationException: An error occurred when trying to authenticate with the server.
at com.auth0.android.authentication.AuthenticationAPIClient$Companion$createErrorAdapter$1.fromJsonResponse(AuthenticationAPIClient.kt:801)
at com.auth0.android.authentication.AuthenticationAPIClient$Companion$createErrorAdapter$1.fromJsonResponse(AuthenticationAPIClient.kt:786)
at com.auth0.android.request.internal.BaseRequest.execute(BaseRequest.kt:113)
at com.auth0.android.request.internal.BaseRequest.start$lambda-2(BaseRequest.kt:68)
at com.auth0.android.request.internal.BaseRequest.$r8$lambda$uH6X7wIol62Lzts5fbwoUeRDLU4(Unknown Source:0)
at com.auth0.android.request.internal.BaseRequest$$ExternalSyntheticLambda2.run(Unknown Source:4)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)

Any help would be very welcome, thanks.

Solved. It was caused by refresh token Reuse Interval being set to 0s in the Mobile App settings in Auth0 Dashboard.

Hi Minarik - I read thru your issue and your fix. Does the Reuse Interval being set to 0s affect individual user access tokens or did this result in an issue for all of your users? Seems it would affect all, but I have a single user who seems to be logged out of our app from time to time due to the access tokens not refreshing and I am not sure what to check on his side since it only affects him. I appreciate any feedback you might have.

It was only causing an issue when requesting the Access Token multiple times a second so it’s use-case dependent.