UNPKG

react-native-biometric-verifier

Version:

A React Native module for biometric verification with face recognition and QR code scanning

72 lines (64 loc) 2.26 kB
import { useCallback } from 'react'; import { Platform, PermissionsAndroid } from 'react-native'; import Geolocation from 'react-native-geolocation-service'; /** * Custom hook for requesting location permission and fetching current location. * * @param {Function} notifyMessage - Callback to show notifications (message, type). * @returns {Object} { requestLocationPermission, getCurrentLocation } */ export const useGeolocation = (notifyMessage) => { /** * Requests location permission (Android only). * @returns {Promise<boolean>} - True if permission granted, else false. */ const requestLocationPermission = useCallback(async () => { if (Platform.OS !== 'android') return true; try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, { title: 'Location Permission Required', message: 'This app needs your location to verify QR code scans.', buttonPositive: 'OK', buttonNegative: 'Cancel', } ); const hasPermission = granted === PermissionsAndroid.RESULTS.GRANTED; if (!hasPermission) { notifyMessage?.('Cannot get location without permission.', 'error'); } return hasPermission; } catch (error) { console.error('Location permission error:', error); notifyMessage?.('Location permission error.', 'error'); return false; } }, [notifyMessage]); /** * Fetches the current location of the device. * @returns {Promise<GeolocationCoordinates>} */ const getCurrentLocation = useCallback( () => new Promise((resolve, reject) => { Geolocation.getCurrentPosition( (position) => { resolve(position.coords); }, (error) => { notifyMessage?.('Unable to fetch current location.', 'error'); reject(error); }, { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000, distanceFilter: 1, } ); }), [notifyMessage] ); return { requestLocationPermission, getCurrentLocation }; };