Expo Local Authentication

55.0.13 · active · verified Wed Apr 22

The `expo-local-authentication` package provides a unified JavaScript API for integrating device-specific biometric authentication methods such as Face ID and Touch ID on iOS, and the Fingerprint API on Android. This module, currently at version 55.0.13, is a core component within the Expo ecosystem, ensuring seamless cross-platform functionality for biometric identification. Its release cadence is tightly coupled with the Expo SDK releases, which typically align with new React Native versions, offering predictable updates and integration. Key differentiators include its deep integration with the Expo development workflow, abstracting away native module complexities, and providing a consistent API for checking hardware availability, user enrollment, and performing biometric authentication across both major mobile platforms. It simplifies the process of adding a crucial security layer to mobile applications developed with Expo and React Native, without requiring direct native code interaction.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the full biometric authentication flow, including checking for hardware availability and user enrollment before attempting to authenticate the user. It also provides basic error handling for common authentication outcomes.

import * as LocalAuthentication from 'expo-local-authentication';
import { Alert, Platform } from 'react-native'; // Assuming a React Native environment for Alert

async function performBiometricAuth() {
  // 1. Check if biometric hardware is available
  const hasHardware = await LocalAuthentication.hasHardwareAsync();
  if (!hasHardware) {
    Alert.alert('Authentication Error', 'Biometric hardware is not available on this device.');
    return;
  }

  // 2. Check if biometrics are enrolled
  const isEnrolled = await LocalAuthentication.isEnrolledAsync();
  if (!isEnrolled) {
    Alert.alert('Authentication Error', 'No biometrics (Face ID/Fingerprint) are enrolled. Please set them up in your device settings.');
    return;
  }

  // 3. Attempt to authenticate
  try {
    const result = await LocalAuthentication.authenticateAsync({
      promptMessage: 'Authenticate to access your account',
      cancelLabel: 'Use Password', // Android specific (optional, customizes button text)
      fallbackLabel: 'Enter Passcode', // iOS specific for older versions (optional)
      disableDeviceFallback: false, // iOS specific (optional, disables passcode fallback)
    });

    if (result.success) {
      Alert.alert('Success!', 'Biometric authentication successful!');
      // Proceed with authenticated user flow
    } else {
      let errorMessage = 'Authentication failed.';
      if (result.error === 'user_cancel') {
        errorMessage = 'Authentication canceled by user.';
      } else if (result.error === 'system_cancel') {
        errorMessage = 'Authentication canceled by system (e.g., app in background).';
      } else if (result.error === 'passcode_not_set') {
        errorMessage = 'Device passcode not set, biometric authentication is not possible.';
      } else if (result.error === 'not_enrolled') {
        errorMessage = 'No biometrics enrolled on the device.';
      } else if (result.error === 'not_available') {
        errorMessage = 'Biometric hardware not available or supported.';
      }
      Alert.alert('Authentication Failed', `${errorMessage} (Error: ${result.error})`);
    }
  } catch (error) {
    console.error('Biometric authentication unexpected error:', error);
    Alert.alert('Error', 'An unexpected error occurred during authentication.');
  }
}

// To integrate this in an Expo/React Native component:
// import React from 'react';
// import { Button, View } from 'react-native';

// const BiometricAuthScreen = () => (
//   <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
//     <Button title="Authenticate with Biometrics" onPress={performBiometricAuth} />
//   </View>
// );

// export default BiometricAuthScreen;

// For immediate testing (e.g., in a standalone script or effect hook):
// (async () => {
//   if (Platform.OS === 'web') {
//     console.warn('Local authentication is not available on web.');
//     return;
//   }
//   // await performBiometricAuth();
// })();

view raw JSON →