Expo 本地认证
一个提供实现指纹 API(Android)或 FaceID 和 TouchID(iOS)功能的库,用于通过人脸或指纹扫描来验证用户身份。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
expo-local-authentication 允许你使用 Biometric Prompt(Android)或 FaceID 和 TouchID(iOS),通过指纹或面部扫描来验证用户身份。
已知限制
iOS iOS
iOS 版 FaceID 身份验证在 Expo Go 中不受支持。你需要创建一个开发构建来测试 FaceID。
安装
- npx expo install expo-local-authenticationIf you are installing this in an existing React Native app, make sure to install expo in your project.
app 配置中的配置
如果你在项目中使用了 config plugins(Continuous Native Generation (CNG)),则可以使用 expo-local-authentication 内置的 config plugin 进行配置。该插件允许你配置一些无法在运行时设置的属性,这些属性需要构建一个新的应用二进制文件后才会生效。如果你的应用不使用 CNG,那么你需要手动配置该库。
Example app.json with config plugin
{ "expo": { "plugins": [ [ "expo-local-authentication", { "faceIDPermission": "Allow $(PRODUCT_NAME) to use Face ID." } ] ] } }
Configurable properties
| Name | Default | Description |
|---|---|---|
faceIDPermission | "Allow $(PRODUCT_NAME) to use Face ID" | Only for: iOS 用于设置 |
Are you using this library in an existing React Native app?
如果你没有使用 Continuous Native Generation(CNG),或者你是手动使用原生 ios 项目,那么你需要将 NSFaceIDUsageDescription 键添加到你的 ios/[app]/Info.plist 中:
<key>NSFaceIDUsageDescription</key> <string>Allow $(PRODUCT_NAME) to use FaceID</string>
API
import * as LocalAuthentication from 'expo-local-authentication';
Methods
| Parameter | Type |
|---|---|
| options(optional) | LocalAuthenticationOptions |
Attempts to authenticate via Fingerprint/TouchID (or FaceID if available on the device).
Note: Apple requires apps which use FaceID to provide a description of why they use this API. If you try to use FaceID on an iPhone with FaceID without providing
infoPlist.NSFaceIDUsageDescriptioninapp.json, the module will authenticate using device passcode. For more information about usage descriptions on iOS, see permissions guide.
Promise<LocalAuthenticationResult>Returns a promise which fulfils with LocalAuthenticationResult.
Cancels authentication flow.
Promise<void>Determine what kind of authentication is enrolled on the device.
Promise<SecurityLevel>Returns a promise which fulfils with SecurityLevel.
Note: On Android devices prior to M,
SECRETcan be returned if only the SIM lock has been enrolled, which is not the method thatauthenticateAsyncprompts.
Determine whether a face or fingerprint scanner is available on the device.
Promise<boolean>Returns a promise which fulfils with a boolean value indicating whether a face or
fingerprint scanner is available on this device.
Determine whether the device has saved fingerprints or facial data to use for authentication.
Promise<boolean>Returns a promise which fulfils to boolean value indicating whether the device has
saved fingerprints or facial data for authentication.
Determine what kinds of authentications are available on the device.
Promise<AuthenticationType[]>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
Literal Type: string
Security level of the biometric authentication to allow.
Acceptable values are: 'weak' | 'strong'
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'
| Property | Type | Description |
|---|---|---|
| biometricsSecurityLevel(optional) | BiometricsSecurityLevel | Only for: Android Sets the security class of biometric authentication to allow.
Default: 'weak' |
| cancelLabel(optional) | string | Allows customizing the default |
| 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
|
| fallbackLabel(optional) | string | Only for: iOS Allows to customize the default |
| 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 |
Type: object shaped as below:
| Property | Type | Description |
|---|---|---|
| success | true | - |
Or object shaped as below:
| Property | Type | Description |
|---|---|---|
| error | LocalAuthenticationError | - |
| success | false | - |
| warning(optional) | string | - |
Enums
SecurityLevel.BIOMETRIC_WEAK = 2Indicates weak biometric authentication. For example, a 2D image-based face unlock.
There are currently no weak biometric authentication options on iOS.
权限
Android
以下权限会通过此库的 AndroidManifest.xml 自动添加:
| Android Permission | Description |
|---|---|
Allows an app to use device supported biometric modalities. | |
|
iOS
以下用途说明键会被此库使用: