Created
July 5, 2018 09:30
-
-
Save ishikota/b64dcbf3106115d8bcdce43518c13007 to your computer and use it in GitHub Desktop.
For medium post https://medium.com/p/b94f656fb05d/edit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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