Capacitor Face ID Plugin
Allow users to authenticate with Face ID or Touch ID on iOS devices.
Installation
npm install capacitor-face-id
Setup
On iOS, you must add an entry to your target’s Info.plist
:
- Go to the settings for your app’s target (not the project) in Xcode.
- Click on the
Info
tab. - Add a property to the properties list.
- Select
Privacy - Face ID Usage Description
as the key. - Set the value to the prompt you want to show to the user the first time a Face/Touch ID authorization is attempted. Example:
Access to the app requires authentication.
Usage
import { Plugins, PluginResultError } from '@capacitor/core';
import { FaceIDPluginErrorCode } from 'capacitor-face-id';
const { FaceId } = Plugins;
const appName = 'My Great App';
async function biometricAuth() {
const authCheck = await FaceId.isAvailable();
const authType = authCheck.value;
if (authType !== 'None') {
try {
await FaceId.auth({
reason: 'Please authenticate',
fallbackTitle: ''
});
} catch (error) {
let message;
switch (error.code) {
// This happens if the user disables biometric access in Settings
case FaceIDPluginErrorCode.biometryNotAvailable:
message = `In order to use ${authType}, you must enable it in Settings > ${appName}.`;
break;
case FaceIDPluginErrorCode.biometryNotEnrolled:
message = `In order to use ${authType}, you must set it up in the Settings app.`;
break;
default:
message = 'Feel free to try again.';
}
await showAlert({
header: `${authType} Error`,
message: `${error.message} ${message}`
});
}
} else {
// use fallback authentication here
}
}
API
The complete API is documented here.