@boindahood/react-native-biometrics
Version:
react native biometrics
147 lines (138 loc) • 4.35 kB
JavaScript
;
import NativeBiometrics, { BiometricType, BiometricErrorCode, BiometricOtherwayMode } from "./NativeBiometrics.js";
export { BiometricType, BiometricErrorCode, BiometricOtherwayMode };
/**
* React Native Biometrics Library
* Provides biometric authentication functionality for iOS and Android
*/
export class ReactNativeBiometrics {
/**
* Check if biometric authentication is available on the device
* @returns Promise<BiometricAvailability>
*/
static async checkBiometricAvailability() {
try {
return await NativeBiometrics.checkBiometricAvailability();
} catch (error) {
return {
isAvailable: false,
allowAccess: false,
biometricType: BiometricType.NONE,
errorCode: BiometricErrorCode.BIOMETRIC_UNKNOWN_ERROR,
errorMessage: error instanceof Error ? error.message : 'Unknown error'
};
}
}
/**
* Request permission to use biometric authentication
* @returns Promise<BiometricPermissionResult>
*/
static async requestBiometricPermission() {
try {
return await NativeBiometrics.requestBiometricPermission();
} catch (error) {
return {
success: false,
errorCode: BiometricErrorCode.BIOMETRIC_UNKNOWN_ERROR,
errorMessage: error instanceof Error ? error.message : 'Unknown error'
};
}
}
/**
* Authenticate using biometric
* @param options - Authentication options
* @returns Promise<BiometricAuthResult>
*/
static async authenticateBiometric(options) {
try {
return await NativeBiometrics.authenticateBiometric(options ?? {}); // fallback for C++ in turbo module
} catch (error) {
return {
success: false,
errorCode: BiometricErrorCode.BIOMETRIC_UNKNOWN_ERROR,
errorMessage: error instanceof Error ? error.message : 'Unknown error'
};
}
}
/**
* Authenticate using device PIN/passcode only
* @returns Promise<BiometricAuthResult>
*/
static async authenticatePIN() {
try {
return await NativeBiometrics.authenticatePIN();
} catch (error) {
return {
success: false,
errorCode: BiometricErrorCode.BIOMETRIC_UNKNOWN_ERROR,
errorMessage: error instanceof Error ? error.message : 'Unknown error'
};
}
}
// Private Key Management Methods
/**
* Create a new biometric private key
* Only one key per app is allowed for security
* @returns Promise<BiometricKeyResult>
*/
static async createBiometricKey() {
try {
return await NativeBiometrics.createBiometricKey();
} catch (error) {
return {
success: false,
errorCode: BiometricErrorCode.BIOMETRIC_UNKNOWN_ERROR,
errorMessage: error instanceof Error ? error.message : 'Unknown error'
};
}
}
/**
* Check if biometric private key exists
* @returns Promise<BiometricKeyExistsResult>
*/
static async biometricKeyExists() {
try {
return await NativeBiometrics.biometricKeyExists();
} catch (error) {
return {
exists: false,
errorCode: BiometricErrorCode.BIOMETRIC_UNKNOWN_ERROR,
errorMessage: error instanceof Error ? error.message : 'Unknown error'
};
}
}
/**
* Delete the biometric private key
* @returns Promise<BiometricKeyResult>
*/
static async deleteBiometricKey() {
try {
return await NativeBiometrics.deleteBiometricKey();
} catch (error) {
return {
success: false,
errorCode: BiometricErrorCode.BIOMETRIC_UNKNOWN_ERROR,
errorMessage: error instanceof Error ? error.message : 'Unknown error'
};
}
}
/**
* Create signature with biometric authentication
* @param payload - The data to sign (will be hashed internally)
* @param options - Authentication options for prompt customization
* @returns Promise<BiometricSignatureResult>
*/
static async createSignature(payload, options) {
try {
return await NativeBiometrics.createSignature(payload, options);
} catch (error) {
return {
success: false,
errorCode: BiometricErrorCode.BIOMETRIC_UNKNOWN_ERROR,
errorMessage: error instanceof Error ? error.message : 'Unknown error'
};
}
}
}
export default ReactNativeBiometrics;
//# sourceMappingURL=index.js.map