Authentication

Example for how to authenticate a user, after they have been enrolled.

🔌 Trigger

Similarly to what you have done for enrolllment, you can request user authentication by invoking the Keyless.getCustomAuthenticationToken() method. You can pass a boolean parameter to decide whether to show the user silhouette in the camera during authentication:

fun onClickAuthenticateButton() {
try {
// true displays the user face during authentication
Keyless.getCustomAuthenticationToken(true)
} catch (e: Exception) {
// Handle exception
}
}

Optionally, get notified when the authentication starts by implementing the optional KeylessDelegate method:

KeylessDelegateCustom.kt
import android.util.Log
import io.keyless.sdk.api.AuthenticationFailed
import io.keyless.sdk.api.DeEnrollmentFailed
import io.keyless.sdk.api.KeylessDelegate
import io.keyless.sdk.api.KeylessException
class KeylessDelegateCustom : KeylessDelegate {
// ...
// Optional functions
override fun authenticationStart() {
// Please make sure to use runOnUiThread() if you need to handle UI changes
Log.i(TAG, "Authentication started.")
}
// ...
}

😎 Result

When authentication finishes, the authenticationFinish method of the KeylessDelegate gets called. Take the token and validate the challenge with it. Handle any errors and prompt the user to try again if it failed. If the process was successful, stop the loading indicator and navigate to the next screen of your application (e.g. payment processed and dismiss view controller):

KeylessDelegateCustom.kt
import android.util.Log
import io.keyless.sdk.api.AuthenticationFailed
import io.keyless.sdk.api.DeEnrollmentFailed
import io.keyless.sdk.api.KeylessDelegate
import io.keyless.sdk.api.KeylessException
class KeylessDelegateCustom : KeylessDelegate {
private var TAG = "KEYLESS_DELEGATE"
override fun authenticationFinish(token: String?, error: AuthenticationFailed?) {
// Please make sure to use runOnUiThread() if you need to handle UI changes
if (error != null) {
Log.e(TAG, "Authentication failed: " + error.title +
" Message: " + error.message + " Code: " + error.code +
" Cancelled by user: " + error.userCancelled);
return
// Show error alert
// prompt user to try again
}
// Handle successful authentication
var authenticationToken: String = token
// This variable will contain "This is a secret token"
}
}