From a theoretical point of view it makes sense. From a practical point of view I don't know the work I'll have to have to abstract that for all the calls in the project. The authenticator it's a life saver in that way.
Let me also share the revalidateToken method:
suspend fun revalidateToken(): String {
return withContext(NonCancellable) {
tokenMutex.lock()
when (val result = service.revalidateToken(getRefreshToken())) {
is Result.Success -> {
updateToken(result.data)
tokenMutex.unlock()
return@withContext result.data.accessToken
}
is Result.Error -> {
tokenMutex.unlock()
throw result.exception
}
}
}
}
I use a mutex to avoid multiple calls invoking the refresh operation. Maybe I'm adding fuel to the fire 😅 but so far it solved the race conditions and I don't recall having a dead lock. But I can't say it's bullet proof either.