Last active
August 16, 2017 15:13
-
-
Save gracietti/3c2b2e0eaf46ad18ce6c4653b368eda4 to your computer and use it in GitHub Desktop.
Call to Auth0 API to get new Access Token based on existing refresh token
This file contains 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
class SessionHelper private constructor() { | |
... | |
fun refreshCredentials(): String? { | |
val refreshToken = refreshToken | |
if (refreshToken != null) { | |
val account = Auth0(String.from(R.string.AUTH0_API_ID), String.from(R.string.AUTH0_API_DOMAIN)) | |
val authentication = AuthenticationAPIClient(account) | |
val response = authentication.renewAuth(refreshToken).execute() | |
response.idToken?.let { saveUserCredentials(accessToken = it) } | |
return response.idToken | |
} else return null | |
} | |
fun saveUserCredentials(accessToken: String, userEmail: String? = null) { | |
this.accessToken = accessToken | |
this.userEmail = userEmail | |
val shared = MyApp.sharedPreferences ?: return | |
val editor = shared.edit() | |
editor.putString(Constants.SharedConfig.accessToken, this.accessToken) | |
userEmail?.let { editor.putString(Constants.SharedConfig.userEmail, this.userEmail) } | |
editor.apply() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment