@willsub-npm/capacitor-face-id

Allow users to authenticate with Face ID or Touch ID on iOS devices


Keywords
capacitor, plugin, native
License
MIT
Install
npm install @willsub-npm/capacitor-face-id@3.0.1

Documentation

Capacitor Face ID Plugin

npm version Build

Allow users to authenticate with Face ID or Touch ID on iOS devices.

Logo

Installation

npm install capacitor-face-id

Setup

On iOS, you must add an entry to your target’s Info.plist:

  1. Go to the settings for your app’s target (not the project) in Xcode.
  2. Click on the Info tab.
  3. Add a property to the properties list.
  4. Select Privacy - Face ID Usage Description as the key.
  5. 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.