79 lines
1.8 KiB
TypeScript
79 lines
1.8 KiB
TypeScript
import ReactNativeBiometrics, {
|
|
BiometryTypes,
|
|
} from 'react-native-biometrics';
|
|
import {
|
|
AuthenticationResult,
|
|
BiometricAvailability,
|
|
BiometricType,
|
|
} from '../types/biometric.types';
|
|
import { BIOMETRIC } from '../utils/constants';
|
|
|
|
const rnBiometrics = new ReactNativeBiometrics();
|
|
|
|
class BiometricService {
|
|
async isAvailable(): Promise<BiometricAvailability> {
|
|
try {
|
|
const { available, biometryType } =
|
|
await rnBiometrics.isSensorAvailable();
|
|
|
|
if (!available) {
|
|
return {
|
|
available: false,
|
|
biometryType: BiometricType.NONE,
|
|
};
|
|
}
|
|
|
|
switch (biometryType) {
|
|
case BiometryTypes.TouchID:
|
|
return {
|
|
available: true,
|
|
biometryType: BiometricType.TOUCH_ID,
|
|
};
|
|
|
|
case BiometryTypes.FaceID:
|
|
return {
|
|
available: true,
|
|
biometryType: BiometricType.FACE_ID,
|
|
};
|
|
|
|
case BiometryTypes.Biometrics:
|
|
return {
|
|
available: true,
|
|
biometryType: BiometricType.BIOMETRICS,
|
|
};
|
|
|
|
default:
|
|
return {
|
|
available: false,
|
|
biometryType: BiometricType.NONE,
|
|
};
|
|
}
|
|
} catch {
|
|
return {
|
|
available: false,
|
|
biometryType: BiometricType.NONE,
|
|
};
|
|
}
|
|
}
|
|
|
|
async authenticate(): Promise<AuthenticationResult> {
|
|
try {
|
|
const { success } = await rnBiometrics.simplePrompt({
|
|
promptMessage: BIOMETRIC.PROMPT_TITLE,
|
|
cancelButtonText: BIOMETRIC.CANCEL_TEXT,
|
|
});
|
|
|
|
return {
|
|
success,
|
|
};
|
|
} catch (error: any) {
|
|
console.log('Biometric Error:', error);
|
|
return {
|
|
success: false,
|
|
error: error?.message ?? 'Authentication failed',
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
export default new BiometricService(); |