Started implementation of biometric (Face ID) authentication
This commit is contained in:
72
src/screen/FaceAuth.tsx
Normal file
72
src/screen/FaceAuth.tsx
Normal file
@@ -0,0 +1,72 @@
|
||||
import React from 'react';
|
||||
import { View, Text, TouchableOpacity, Alert, StyleSheet } from 'react-native';
|
||||
import ReactNativeBiometrics from 'react-native-biometrics';
|
||||
|
||||
const FaceAuth = () => {
|
||||
|
||||
const authenticate = async () => {
|
||||
const rnBiometrics = new ReactNativeBiometrics();
|
||||
|
||||
const { available, biometryType } = await rnBiometrics.isSensorAvailable();
|
||||
|
||||
if (!available) {
|
||||
Alert.alert('Error', 'Biometric authentication not available');
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
biometryType === ReactNativeBiometrics.FaceID ||
|
||||
biometryType === ReactNativeBiometrics.Biometrics
|
||||
) {
|
||||
rnBiometrics.simplePrompt({
|
||||
promptMessage: 'Authenticate with Face ID',
|
||||
})
|
||||
.then((resultObject) => {
|
||||
const { success } = resultObject;
|
||||
|
||||
if (success) {
|
||||
Alert.alert('Success', 'Face authentication successful');
|
||||
// 👉 navigate / unlock app / allow action
|
||||
} else {
|
||||
Alert.alert('Cancelled', 'User cancelled authentication');
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
Alert.alert('Error', 'Authentication failed');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<View style={styles.container}>
|
||||
<Text style={styles.title}>Face Authentication</Text>
|
||||
|
||||
<TouchableOpacity style={styles.button} onPress={authenticate}>
|
||||
<Text style={styles.buttonText}>Authenticate</Text>
|
||||
</TouchableOpacity>
|
||||
</View>
|
||||
);
|
||||
};
|
||||
|
||||
export default FaceAuth;
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
container: {
|
||||
flex: 1,
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center'
|
||||
},
|
||||
title: {
|
||||
fontSize: 20,
|
||||
marginBottom: 20
|
||||
},
|
||||
button: {
|
||||
backgroundColor: '#4CAF50',
|
||||
padding: 15,
|
||||
borderRadius: 8
|
||||
},
|
||||
buttonText: {
|
||||
color: '#fff',
|
||||
fontWeight: 'bold'
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user