Skip to content

Instantly share code, notes, and snippets.

@ishikota
Created July 5, 2018 09:30
Show Gist options
  • Save ishikota/b64dcbf3106115d8bcdce43518c13007 to your computer and use it in GitHub Desktop.
Save ishikota/b64dcbf3106115d8bcdce43518c13007 to your computer and use it in GitHub Desktop.
import okhttp3.Authenticator
class MyAppAuthenticator(private val preference: SharedPreferences): Authenticator {
// This callback is called when api request failed in 401.
override fun authenticate(route: Route?, response: Response?): Request? {
val currentRefreshToken = preference.getString("REFRESH_TOKEN_KEY", "")
val (newAccessToken, newRefreshToken) = requestNewTokensToServer(currentRefreshToken).blockingGet() // TODO error handling
preference.edit().putString("ACCESS_TOKEN_KEY", newAccessToken).apply()
preference.edit().putString("REFRESH_TOKEN_KEY", newRefreshToken).apply()
return attachNewTokenOnRequest(response.request(), newAccessToken)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment