Skip to content

Instantly share code, notes, and snippets.

@nicbell
Created July 7, 2019 11:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicbell/260f6d5027e055b8f91c7521ba70947e to your computer and use it in GitHub Desktop.
Save nicbell/260f6d5027e055b8f91c7521ba70947e to your computer and use it in GitHub Desktop.
Async wrapper for Auth0.
import android.content.Context
import com.auth0.android.Auth0
import com.auth0.android.authentication.AuthenticationAPIClient
import com.auth0.android.authentication.AuthenticationException
import com.auth0.android.callback.BaseCallback
import com.auth0.android.result.Credentials
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Deferred
import ltd.inja.app.R
/**
* Async to wrapper for Auth0 so that we we can use coroutines rather than callback hell.
*/
class Auth0Async(private val context: Context) {
private var account: Auth0 = Auth0(context)
init {
account.isOIDCConformant = true
}
fun loginAsync(email: String, password: String, error: (AuthenticationException) -> Unit): Deferred<Credentials?> {
val deferred = CompletableDeferred<Credentials?>()
AuthenticationAPIClient(account)
.login(email, password, context.getString(R.string.auth0_connection))
.setScope(context.getString(R.string.auth0_scope))
.setAudience(context.getString(R.string.com_auth0_audience))
.start(object : BaseCallback<Credentials, AuthenticationException> {
override fun onSuccess(payload: Credentials?) {
deferred.complete(payload)
}
override fun onFailure(exception: AuthenticationException) {
//deferred.completeExceptionally(error)
deferred.complete(null)
error.invoke(exception)
}
})
return deferred
}
fun resetPasswordAsync(email: String, error: (AuthenticationException) -> Unit): Deferred<Void?> {
val deferred = CompletableDeferred<Void?>()
AuthenticationAPIClient(account)
.resetPassword(email, context.getString(R.string.auth0_connection))
.start(object : BaseCallback<Void, AuthenticationException> {
override fun onSuccess(payload: Void?) {
deferred.complete(payload)
}
override fun onFailure(exception: AuthenticationException) {
deferred.complete(null)
error.invoke(exception)
}
})
return deferred
}
fun createUserAsync(email: String, password: String, error: (AuthenticationException) -> Unit): Deferred<Credentials?> {
val deferred = CompletableDeferred<Credentials?>()
AuthenticationAPIClient(account)
.signUp(email, password, context.getString(R.string.auth0_connection))
.setScope(context.getString(R.string.auth0_scope))
.setAudience(context.getString(R.string.com_auth0_audience))
.start(object : BaseCallback<Credentials, AuthenticationException> {
override fun onSuccess(payload: Credentials?) {
deferred.complete(payload)
}
override fun onFailure(exception: AuthenticationException) {
deferred.complete(null)
error.invoke(exception)
}
})
return deferred
}
fun renewAuthAsync(refreshToken: String, error: (AuthenticationException) -> Unit): Deferred<Credentials?> {
val deferred = CompletableDeferred<Credentials?>()
AuthenticationAPIClient(account)
.renewAuth(refreshToken)
.start(object : BaseCallback<Credentials, AuthenticationException> {
override fun onSuccess(payload: Credentials?) {
deferred.complete(payload)
}
override fun onFailure(exception: AuthenticationException) {
deferred.complete(null)
error.invoke(exception)
}
})
return deferred
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment