LocalAuth
The LocalAuth
API is a wrapper around the iOS Local Authentication framework, enabling biometric or passcode authentication in your Scripting app scripts. This document explains how to use the LocalAuth
API effectively.
Overview
The LocalAuth
module provides methods and properties for checking authentication availability and performing user authentication. It supports biometrics like Face ID, Touch ID, and Optic ID, as well as fallback options like passcodes.
Properties
LocalAuth.isAvailable
- Type:
boolean
- Description: Indicates whether authentication can proceed using any available policies.
- Example:
1if (LocalAuth.isAvailable) {
2 console.log("Authentication is available.")
3} else {
4 console.log("Authentication is not available.")
5}
LocalAuth.isBiometricsAvailable
- Type:
boolean
- Description: Indicates whether biometric authentication can proceed.
- Example:
1if (LocalAuth.isBiometricsAvailable) {
2 console.log("Biometric authentication is available.")
3} else {
4 console.log("Biometric authentication is not available.")
5}
LocalAuth.biometryType
- Type:
LocalAuthBiometryType
- Description: Specifies the type of biometric authentication supported by the device. Possible values:
"faceID"
"touchID"
"opticID"
"none"
"unknown"
- Example:
1const biometry = LocalAuth.biometryType
2console.log(`Biometry type: ${biometry}`)
Methods
LocalAuth.authenticate(reason: string, useBiometrics?: boolean): Promise<boolean>
- Description: Authenticates the user with available biometrics or a fallback method (e.g., passcode). Returns a promise that resolves to
true
if authentication succeeds, or false
if it fails.
- Parameters:
reason
(string): The message displayed to the user when prompting for authentication. This must not be empty. Example: 'Authenticate to access MyScript.'
useBiometrics
(boolean, optional): Defaults to true
. If true
, the method uses biometrics for authentication otherwise, it uses biometrics or a fallback method (e.g., passcode).
- Example:
1async function authenticateUser() {
2 const reason = "Authenticate to access MyScript."
3 const result = await LocalAuth.authenticate(reason, true)
4 if (result) {
5 console.log("Authentication succeeded.")
6 } else {
7 console.log("Authentication failed.")
8 }
9}
10
11authenticateUser()
Usage Examples
Check Biometric Availability
1if (LocalAuth.isBiometricsAvailable) {
2 console.log("Device supports biometric authentication.")
3 console.log(`Biometry type: ${LocalAuth.biometryType}`)
4} else {
5 console.log("Biometric authentication is not supported on this device.")
6}
Authenticate with Biometrics
1async function accessSecureData() {
2 const authenticated = await LocalAuth.authenticate(
3 "Authenticate to access secure data."
4 )
5 if (authenticated) {
6 console.log("Access granted.")
7 } else {
8 console.log("Access denied.")
9 }
10}
11
12accessSecureData()
Fallback to Passcode Authentication
1async function authenticateWithFallback() {
2 const authenticated = await LocalAuth.authenticate(
3 "Authenticate to proceed.",
4 false // Allow biometrics or passcode
5 )
6 console.log(authenticated ? "Authenticated" : "Authentication failed")
7}
8
9authenticateWithFallback()
Notes
- Always provide a meaningful message in the
reason
parameter to help users understand why authentication is being requested.
- Use
LocalAuth.isAvailable
and LocalAuth.isBiometricsAvailable
to check the availability of authentication options before invoking LocalAuth.authenticate
.
- Handle both success and failure cases gracefully to provide a seamless user experience.