Skip to content

Instantly share code, notes, and snippets.

@adrianhall
Created September 13, 2018 19:41
Show Gist options
  • Save adrianhall/08a84ad69d2cd7bfdc978ef902f6df7a to your computer and use it in GitHub Desktop.
Save adrianhall/08a84ad69d2cd7bfdc978ef902f6df7a to your computer and use it in GitHub Desktop.
@SuppressLint("InflateParams")
private fun handleLogin() {
model.initiateSignin { identityRequest, params, callback ->
when(identityRequest) {
IdentityRequest.NEED_CREDENTIALS -> {
callback(mapOf(
"username" to loginFormUsernameField.text.toString(),
"password" to loginFormPasswordField.text.toString()
))
}
IdentityRequest.NEED_MULTIFACTORCODE -> {
val mfaDialog = layoutInflater.inflate(R.layout.dialog_multifactor_auth, null)
val mfaCodeInput = mfaDialog.find(R.id.dialog_mfa_code) as EditText
val mfaInstructions = mfaDialog.find(R.id.dialog_mfa_instructions) as TextView
params?.let {
val deliveryTo = it.getOrDefault("deliveryTo", "UNKNOWN")
mfaInstructions.text = getString(R.string.specific_mfa_instructions, deliveryTo)
}
alert {
title = "Enter Secondary Code"
customView = mfaDialog
positiveButton("OK") { callback(mapOf("mfaCode" to mfaCodeInput.text.toString())) }
}.show()
}
IdentityRequest.NEED_NEWPASSWORD -> {
val newPasswordDialog = layoutInflater.inflate(R.layout.dialog_new_password, null)
val passwordInput = newPasswordDialog.find(R.id.new_password_form_password) as EditText
alert {
title = "Enter New Password"
customView = newPasswordDialog
positiveButton("OK") { callback(mapOf("password" to passwordInput.text.toString())) }
}.show()
}
IdentityRequest.SUCCESS -> {
analyticsService.recordSuccessfulLogin(loginFormUsernameField.text.toString())
[email protected]()
}
IdentityRequest.FAILURE -> {
analyticsService.recordFailedLogin()
alert(params?.get("message") ?: "Error submitting credentials") {
title = "Login Denied"
positiveButton("Close") { /* Do Nothing */ }
}.show()
}
else -> {
analyticsService.recordEvent("ERROR", mapOf("error" to "unexpected identity request"))
alert("Unknown or unexpected identity request") {
title = "Something went wrong!"
positiveButton("Close") { /* Do Nothing */ }
}.show()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment