Skip to content

Instantly share code, notes, and snippets.

@adrianhall
Created April 18, 2018 22:34
Show Gist options
  • Save adrianhall/229f21fbe73833a44ec76b0c9616316f to your computer and use it in GitHub Desktop.
Save adrianhall/229f21fbe73833a44ec76b0c9616316f to your computer and use it in GitHub Desktop.
The refactored handleLogin() method
@SuppressLint("InflateParams")
private fun handleLogin() {
identityManager.initiateSignin {
request, params, callback -> when(request) {
IdentityRequest.NEED_CREDENTIALS -> {
Log.d(TAG, "NEED_CREDENTIALS")
callback(mapOf("username" to loginform_username.getContent(), "password" to loginform_password.getContent()))
}
IdentityRequest.NEED_NEWPASSWORD -> {
Log.d(TAG, "NEED_NEWPASSWORD")
val newPasswordDialog = layoutInflater.inflate(R.layout.dialog_new_password, null)
val passwordInput = newPasswordDialog.find(R.id.newpassworddialog_password) as EditText
alert {
title = "Enter New Password"
customView = newPasswordDialog
positiveButton("OK") {
callback(mapOf("password" to passwordInput.getContent()))
}
}.show()
}
IdentityRequest.NEED_MULTIFACTORCODE -> {
Log.d(TAG, "NEED_MULTIFACTORCODE")
val mfaPromptDialog = layoutInflater.inflate(R.layout.dialog_mfa_prompt, null)
val mfaCodeInput = mfaPromptDialog.find(R.id.mfapromptdialog_code) as EditText
val mfaInstructions = mfaPromptDialog.find(R.id.mfapromptdialog_instructions) as TextView
params?.let { mfaInstructions.text = "Enter the code we just sent to ${it["deliveryVia"] ?: "UNK"}:${it["deliveryTo"] ?: "UNKNOWN"}" }
alert {
title = "Multi-factor Code Required"
customView = mfaPromptDialog
positiveButton("OK") {
callback(mapOf("mfaCode" to mfaCodeInput.getContent()))
}
}.show()
}
// Sucessful signin
IdentityRequest.SUCCESS -> {
Log.d(TAG, "SUCCESS")
[email protected]()
}
// Failed signin
IdentityRequest.FAILURE -> {
Log.d(TAG, "FAILURE")
alert(params?.get("message") ?: "Error submitting credentials") {
title = "Login Denied"
positiveButton("Close") { /* Do nothing */ }
}.show()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment