48 lines
1.0 KiB
TypeScript
48 lines
1.0 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react';
|
|
import biometricService from '../services/biometricService';
|
|
import {
|
|
BiometricType,
|
|
UseBiometricReturn,
|
|
} from '../types/biometric.types';
|
|
|
|
export default function useBiometric(): UseBiometricReturn {
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [isAvailable, setIsAvailable] =
|
|
useState(false);
|
|
|
|
const [biometryType, setBiometryType] =
|
|
useState<BiometricType>(BiometricType.NONE);
|
|
|
|
const checkAvailability = useCallback(async () => {
|
|
const result =
|
|
await biometricService.isAvailable();
|
|
|
|
setIsAvailable(result.available);
|
|
|
|
setBiometryType(result.biometryType);
|
|
}, []);
|
|
|
|
const authenticate = useCallback(async () => {
|
|
setLoading(true);
|
|
|
|
const result =
|
|
await biometricService.authenticate();
|
|
|
|
setLoading(false);
|
|
|
|
return result.success;
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
checkAvailability();
|
|
}, [checkAvailability]);
|
|
|
|
return {
|
|
loading,
|
|
isAvailable,
|
|
biometryType,
|
|
authenticate,
|
|
checkAvailability,
|
|
};
|
|
} |