Reference version

This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo LocalAuthentication iconExpo LocalAuthentication

一个库,提供实现指纹 API(Android)或 FaceID 和 TouchID(iOS)的功能,以通过面部或指纹扫描验证用户身份

Android
iOS
Included in Expo Go
Recommended version:
~58.0.0

expo-local-authentication 允许你使用生物识别提示(Android)或 FaceID 和 TouchID(iOS),通过指纹或面部扫描对用户进行身份验证。

已知限制

iOS 
iOS

iOS 的 FaceID 身份验证不支持在 Expo Go 中使用。你需要创建一个开发版本来测试 FaceID。

安装

Terminal
- npx expo install expo-local-authentication
- yarn expo install expo-local-authentication
- pnpm expo install expo-local-authentication
- bun expo install expo-local-authentication

If you are installing this in an existing React Native app, make sure to install expo in your project.

在应用配置中进行配置

如果你的项目使用配置插件(持续原生生成(CNG)),则可以使用内置的配置插件来配置 expo-local-authentication。该插件允许你配置无法在运行时设置、且需要构建新的应用二进制文件才能生效的各种属性。如果你的应用不使用 CNG,则需要手动配置该库。

Example app.json with config plugin

app.json
{ "expo": { "plugins": [ [ "expo-local-authentication", { "faceIDPermission": "Allow $(PRODUCT_NAME) to use Face ID." } ] ] } }

Configurable properties

NameDefaultDescription
faceIDPermission"Allow $(PRODUCT_NAME) to use Face ID"
Only for: 
iOS

用于设置 NSFaceIDUsageDescription 权限说明的字符串。如果你的应用不使用 Face ID,请将其设为 false,以便从 Info.plist 中省略 NSFaceIDUsageDescription。

Are you using this library in an existing React Native app?

如果你不使用持续原生生成(CNG),或者手动使用原生 ios 项目,则需要将 NSFaceIDUsageDescription 键添加到 ios/[app]/Info.plist 中:

Info.plist
<key>NSFaceIDUsageDescription</key> <string>Allow $(PRODUCT_NAME) to use FaceID</string>

API

import * as LocalAuthentication from 'expo-local-authentication';

Methods

LocalAuthentication.authenticateAsync(options)

Android
iOS
ParameterType
options(optional)LocalAuthenticationOptions

Attempts to authenticate via Fingerprint/TouchID (or FaceID if available on the device).

Returns a promise which fulfils with LocalAuthenticationResult.

LocalAuthentication.cancelAuthenticate()

Android

Cancels authentication flow.

Returns:
Promise<void>

LocalAuthentication.getEnrolledLevelAsync()

Android
iOS

Determine what kind of authentication is enrolled on the device.

Returns a promise which fulfils with SecurityLevel.

LocalAuthentication.hasHardwareAsync()

Android
iOS

Determine whether a face or fingerprint scanner is available on the device.

Returns:
Promise<boolean>

Returns a promise which fulfils with a boolean value indicating whether a face or fingerprint scanner is available on this device.

LocalAuthentication.isEnrolledAsync()

Android
iOS

Determine whether the device has saved fingerprints or facial data to use for authentication.

Returns:
Promise<boolean>

Returns a promise which fulfils to boolean value indicating whether the device has saved fingerprints or facial data for authentication.

LocalAuthentication.supportedAuthenticationTypesAsync()

Android
iOS

Determine what kinds of authentications are available on the device.

Returns a promise which fulfils to an array containing AuthenticationTypes.

Devices can support multiple authentication methods - i.e. [1,2] means the device supports both fingerprint and facial recognition. If none are supported, this method returns an empty array.

Types

BiometricsSecurityLevel

Android

Literal type: string

Security level of the biometric authentication to allow.

Acceptable values are: 'weak' | 'strong'

LocalAuthenticationError

Android
iOS

Literal type: string

One of the error values returned by the LocalAuthenticationResult object.

Acceptable values are: 'not_enrolled' | 'user_cancel' | 'app_cancel' | 'not_available' | 'lockout' | 'no_space' | 'timeout' | 'unable_to_process' | 'unknown' | 'system_cancel' | 'user_fallback' | 'invalid_context' | 'passcode_not_set' | 'authentication_failed'

LocalAuthenticationOptions

Android
iOS
PropertyTypeDescription
biometricsSecurityLevel(optional)BiometricsSecurityLevel
Only for: 
Android

Sets the security class of biometric authentication to allow. strong allows only Android Class 3 biometrics. For example, a fingerprint or a 3D face scan. weak allows both Android Class 3 and Class 2 biometrics. Class 2 biometrics are less secure than Class 3. For example, a camera-based face unlock.

Default:'weak'
cancelLabel(optional)string

Allows customizing the default Cancel label shown.

disableDeviceFallback(optional)boolean

After several failed attempts, the system falls back to the device passcode. This setting allows you to disable this option and instead handle the fallback yourself. This can be preferable in certain custom authentication workflows. This behaviour maps to using the iOS LAPolicyDeviceOwnerAuthenticationWithBiometrics policy rather than the LAPolicyDeviceOwnerAuthentication policy. Defaults to false.

fallbackLabel(optional)string
Only for: 
iOS

Allows to customize the default Use Passcode label shown after several failed authentication attempts. Setting this option to an empty string disables this button from showing in the prompt.

promptDescription(optional)string
Only for: 
Android

A description displayed in the middle of the authentication prompt.

promptMessage(optional)string

A message that is shown alongside the TouchID or FaceID prompt.

promptSubtitle(optional)string
Only for: 
Android

A subtitle displayed below the prompt message in the authentication prompt.

requireConfirmation(optional)boolean
Only for: 
Android

Sets a hint to the system for whether to require user confirmation after authentication. This may be ignored by the system if the user has disabled implicit authentication in Settings or if it does not apply to a particular biometric modality. Defaults to true.

LocalAuthenticationResult

Android
iOS

Type: object shaped as below:

PropertyTypeDescription
successtrue
-

Or object shaped as below:

PropertyTypeDescription
errorLocalAuthenticationError
-
successfalse
-
warning(optional)string
-

Enums

AuthenticationType

Android
iOS

FINGERPRINT

AuthenticationType.FINGERPRINT = 1

Indicates fingerprint support.

FACIAL_RECOGNITION

AuthenticationType.FACIAL_RECOGNITION = 2

Indicates facial recognition support.

IRIS

Android
AuthenticationType.IRIS = 3

Indicates iris recognition support.

SecurityLevel

Android
iOS

NONE

SecurityLevel.NONE = 0

Indicates no enrolled authentication.

SECRET

SecurityLevel.SECRET = 1

Indicates non-biometric authentication (e.g. PIN, Pattern).

BIOMETRIC_WEAK

SecurityLevel.BIOMETRIC_WEAK = 2

Indicates weak biometric authentication. For example, a 2D image-based face unlock.

BIOMETRIC_STRONG

SecurityLevel.BIOMETRIC_STRONG = 3

Indicates strong biometric authentication. For example, a fingerprint scan or 3D face unlock.

权限

Android

此库会自动通过其 AndroidManifest.xml 添加以下权限:

Android permissionDescription

USE_BIOMETRIC

Allows an app to use device supported biometric modalities.

USE_FINGERPRINT

iOS

此库使用以下使用说明键:

Info.plist keyDescription

NSFaceIDUsageDescription

A message that tells the user why the app is requesting the ability to authenticate with Face ID.