UNPKG

react-native-biometry

Version:
82 lines (81 loc) 2.4 kB
import { NativeModules } from 'react-native'; import { UnavailableError } from './unavailableError'; const { Biometry: RNBiometry } = NativeModules; const defaultPromptOptions = { title: 'Authentication required', subtitle: '', description: '', cancelText: 'Cancel', }; export var SecurityLevel; (function (SecurityLevel) { SecurityLevel[SecurityLevel["NONE"] = 0] = "NONE"; SecurityLevel[SecurityLevel["SECRET"] = 1] = "SECRET"; SecurityLevel[SecurityLevel["BIOMETRIC"] = 2] = "BIOMETRIC"; })(SecurityLevel || (SecurityLevel = {})); /** @public */ export class Biometry { static async isAvailable() { if (!RNBiometry?.isAvailable) { throw new UnavailableError(); } return RNBiometry.isAvailable(); } static async isEnrolled() { if (!RNBiometry?.isEnrolled) { throw new UnavailableError(); } return RNBiometry.isEnrolled(); } static async getEnrolledLevel() { if (!RNBiometry?.isEnrolled) { throw new UnavailableError(); } return RNBiometry.getEnrolledLevel(); } static async encryptData(params) { if (!RNBiometry?.encryptData) { throw new UnavailableError(); } try { return await RNBiometry.encryptData(params); } catch (e) { return { data: null, success: false, }; } } static async authenticate(options = {}) { if (!RNBiometry?.authenticate) { throw new UnavailableError(); } try { const prompt = { ...defaultPromptOptions, ...(options.prompt || {}), }; return RNBiometry.authenticate({ prompt, encryptedData: options.encryptedData?.length ? options.encryptedData : null, securityKeyName: options.securityKeyName?.length ? options.securityKeyName : null, }); } catch (e) { return { success: false, data: null, error: e.message, }; } } static async cancel() { if (!RNBiometry?.cancel) { throw new UnavailableError(); } return RNBiometry.cancel(); } }