I have multple API calls in Parallel Mod in one View, and when token is Expired all of them got 401, and authenticator refreshing token for all of them. how should I protect myself against this. I've heard about mutex but I don't really know how to do it with it. Is it better to authorize the 401 using Authenticator or interceptor or are there any other ways to handle this?
My authenticator class below.
class TokenAuthenticator(
private val authenticationRepository: AuthenticationRepository,
private val userAndTokenRepository: UserAndTokenRepository,
private val crashlyticsManager: CrashlyticsManager
) : Authenticator {
override fun authenticate(route: Route?, response: Response): Request? {
return runBlocking {
getUpdatedToken()?.let { token ->
userAndTokenRepository.storeToken(token)
response.request.newBuilder()
.header(HEADER_AUTHORIZATION, "$AUTHORIZATION_TYPE ${token.accessToken}")
.build()
}
}
}
private suspend fun getUpdatedToken(): Token? {
return withContext(Dispatchers.IO) {
val accessTokenResult = authenticationRepository.refresh(
userAndTokenRepository.getEmail() ?: "",
userAndTokenRepository.getRefreshToken() ?: ""
)
when (accessTokenResult) {
is Result.Success -> accessTokenResult.data
is Result.Failure -> {
crashlyticsManager.recordException(Exception(accessTokenResult.error))
null
}
}
}
}
}