Skip to content

Instantly share code, notes, and snippets.

@gracietti
Last active August 16, 2017 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gracietti/3c2b2e0eaf46ad18ce6c4653b368eda4 to your computer and use it in GitHub Desktop.
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
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