--

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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Guilherme Delgado
Guilherme Delgado

Written by Guilherme Delgado

Software Engineer at BlissApplications 🇵🇹 • github.com/GuilhE • When I’m not coding I’m 🧗🏽or 🏄‍♂️

No responses yet

Write a response