Files
heval/src/services/biometrics.ts
2025-07-15 20:38:04 +02:00

67 lines
1.8 KiB
TypeScript

import * as LocalAuthentication from "expo-local-authentication";
class BiometricsService {
async isBiometricSupported(): Promise<boolean> {
try {
const compatible = await LocalAuthentication.hasHardwareAsync();
const enrolled = await LocalAuthentication.isEnrolledAsync();
return compatible && enrolled;
} catch (error) {
console.error("Biometric support check failed:", error);
return false;
}
}
async getBiometricType(): Promise<LocalAuthentication.AuthenticationType[]> {
try {
const types =
await LocalAuthentication.supportedAuthenticationTypesAsync();
return types;
} catch (error) {
console.error("Get biometric type failed:", error);
return [];
}
}
async authenticateWithBiometrics(): Promise<boolean> {
try {
const biometryType = await this.isBiometricSupported();
if (!biometryType) {
throw new Error("Biometric authentication not supported");
}
const result = await LocalAuthentication.authenticateAsync({
promptMessage: "Biometrische Authentifizierung",
cancelLabel: "Abbrechen",
fallbackLabel: "Passcode verwenden",
requireConfirmation: true,
});
return result.success;
} catch (error) {
console.error("Biometric authentication failed:", error);
return false;
}
}
async showBiometricPrompt(
reason: string = "Für sicheren Zugang authentifizieren"
): Promise<boolean> {
try {
const isSupported = await this.isBiometricSupported();
if (!isSupported) {
throw new Error("Biometric authentication not available");
}
return await this.authenticateWithBiometrics();
} catch (error) {
console.error("Biometric prompt failed:", error);
return false;
}
}
}
export default new BiometricsService();