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:
1
fun onClickAuthenticateButton() {
2
try {
3
// true displays the user face during authentication
4
Keyless.getCustomAuthenticationToken(true)
5
} catch (e: Exception) {
6
// Handle exception
7
}
8
}
Copied!
Optionally, get notified when the authentication starts by implementing the optional KeylessDelegate method:
KeylessDelegateCustom.kt
1
import android.util.Log
2
import io.keyless.sdk.api.AuthenticationFailed
3
import io.keyless.sdk.api.DeEnrollmentFailed
4
import io.keyless.sdk.api.KeylessDelegate
5
import io.keyless.sdk.api.KeylessException
6
​
7
class KeylessDelegateCustom : KeylessDelegate {
8
​
9
// ...
10
11
// Optional functions
12
​
13
override fun authenticationStart() {
14
// Please make sure to use runOnUiThread() if you need to handle UI changes
15
Log.i(TAG, "Authentication started.")
16
}
17
​
18
// ...
19
}
Copied!

​
😎
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
1
import android.util.Log
2
import io.keyless.sdk.api.AuthenticationFailed
3
import io.keyless.sdk.api.DeEnrollmentFailed
4
import io.keyless.sdk.api.KeylessDelegate
5
import io.keyless.sdk.api.KeylessException
6
​
7
class KeylessDelegateCustom : KeylessDelegate {
8
​
9
private var TAG = "KEYLESS_DELEGATE"
10
​
11
override fun authenticationFinish(token: String?, error: AuthenticationFailed?) {
12
// Please make sure to use runOnUiThread() if you need to handle UI changes
13
if (error != null) {
14
Log.e(TAG, "Authentication failed: " + error.title +
15
" Message: " + error.message + " Code: " + error.code +
16
" Cancelled by user: " + error.userCancelled);
17
return
18
19
// Show error alert
20
// prompt user to try again
21
}
22
​
23
// Handle successful authentication
24
var authenticationToken: String = token
25
// This variable will contain "This is a secret token"
26
}
27
​
28
}
Copied!
Export as PDF
Copy link