import { Platform } from 'react-native'; import ReactNativeBiometrics, { type BiometryType, BiometryTypes, } from 'react-native-biometrics'; const rnBiometrics = new ReactNativeBiometrics(); interface IsSensorAvailableResult { available: boolean; biometryType?: BiometryType; error?: string; } enum BiometricErrorAndroid { NO_HARDWARE = 'BIOMETRIC_ERROR_NO_HARDWARE', HW_UNAVAILBLE = 'BIOMETRIC_ERROR_HW_UNAVAILABLE', NONE_ENROLLED = 'BIOMETRIC_ERROR_NONE_ENROLLED', } /* checking if biometric sensors are enabled on device */ export default class BioMetricManager { static checkSensorAvailable() { return new Promise((resolve, reject) => { try { rnBiometrics .isSensorAvailable() .then((resultObject: IsSensorAvailableResult) => { const { available, biometryType, error } = resultObject; if (available && biometryType === BiometryTypes.TouchID) { resolve('TouchID'); } else if (available && biometryType === BiometryTypes.FaceID) { resolve('FaceID'); } else if (available && biometryType === BiometryTypes.Biometrics) { resolve('Biometrics'); } else { if (Platform.OS === 'android') { if (error === BiometricErrorAndroid.NO_HARDWARE) { reject(error); } else if (error === BiometricErrorAndroid.NONE_ENROLLED) { reject(error); } else if (error === BiometricErrorAndroid.HW_UNAVAILBLE) { reject(error); } } else { reject('Not Available'); } } }); } catch (err) { reject('Not Available'); } }); } /* Propmpts dialogue to user for using fingerprint or faceid */ static displayPrompt(msg: string) { return new Promise((resolve, reject) => { try { rnBiometrics .simplePrompt({ promptMessage: msg }) .then((res: any) => { const { success } = res; if (success) { resolve(true); } else { reject(false); } }) .catch(() => { reject(false); }); } catch (error) { reject(false); } }); } }