Biometrics and Sensors

Article · 3:00

Face ID and Touch ID done so they cannot be bypassed, plus the accelerometer and haptics.

This lesson has 1 runnable example. Press Try yourself! under any of them to open it in the editor.

expo-local-authentication gives you Face ID, Touch ID and Android's biometric prompt behind one call. The important thing to understand is what it returns: a boolean. It tells you the device verified its owner. It does not hand you a key, and on its own it secures nothing - code that does if (success) { showSecrets() } can be bypassed by anyone who can patch the JavaScript bundle.

The correct pattern is to let biometrics gate access to something the system holds. Store the token in SecureStore with requireAuthentication, so the operating system will not return the bytes until the owner has been verified. Then the check is enforced by the Keychain, not by your if.

Always offer a fallback. Hardware may be absent, nothing may be enrolled, and repeated failures lock out the sensor - DeviceOwnerAuth lets the system fall back to the passcode, and an app with no alternative path is an app someone cannot get into.

The sensors are simpler. expo-sensors exposes the accelerometer, gyroscope and magnetometer as subscriptions with a configurable interval - set it deliberately, because the default is faster than most uses need and costs battery. expo-haptics gives you the taps and thuds that make an interaction feel physical; use the semantic ones, and sparingly.

TS Example 1 57 lines
import * as LocalAuthentication from 'expo-local-authentication';
import * as SecureStore from 'expo-secure-store';
import * as Haptics from 'expo-haptics';
import { Accelerometer } from 'expo-sensors';
import { useEffect, useState } from 'react';

// WRONG: the boolean is advisory. Patch the bundle and this is bypassed.
//   const { success } = await LocalAuthentication.authenticateAsync();
//   if (success) return secrets;

// RIGHT: the system will not hand back the bytes until it has verified
// the owner. The check is enforced by the Keychain, not by this function.
export async function readTokenBehindBiometrics(): Promise<string | null> {
  const hasHardware = await LocalAuthentication.hasHardwareAsync();
  const enrolled = await LocalAuthentication.isEnrolledAsync();

  if (!hasHardware || !enrolled) {
    // No biometrics on this device - fall back, do not lock the user out.
    return SecureStore.getItemAsync('accessToken');
  }

  return SecureStore.getItemAsync('accessToken', {
    requireAuthentication: true,
    authenticationPrompt: 'Unlock to see your account',
  });
}

export async function confirmPayment(): Promise<boolean> {
  const result = await LocalAuthentication.authenticateAsync({
    promptMessage: 'Confirm this payment',
    // Lets the system fall back to the device passcode after failures.
    disableDeviceFallback: false,
  });

  await Haptics.notificationAsync(
    result.success ? Haptics.NotificationFeedbackType.Success
                   : Haptics.NotificationFeedbackType.Error,
  );
  return result.success;
}

export function useShakeDetector(onShake: () => void) {
  const [subscribed, setSubscribed] = useState(false);

  useEffect(() => {
    // Set this deliberately. The default is faster than most uses need.
    Accelerometer.setUpdateInterval(200);

    const subscription = Accelerometer.addListener(({ x, y, z }) => {
      if (Math.sqrt(x * x + y * y + z * z) > 1.8) onShake();
    });
    setSubscribed(true);
    return () => subscription.remove();
  }, [onShake]);

  return subscribed;
}

Open this example in the editor and change it. Nothing you type is saved.