Added OS-level authentication with optional App PIN / Offline PIN fallback

This commit is contained in:
mansi-dev
2026-01-15 23:07:06 +05:30
parent 250f9286d6
commit 9660a3267c
3 changed files with 76 additions and 1 deletions

View File

@@ -0,0 +1,35 @@
import React from 'react';
import { View, Text, TouchableOpacity, Alert } from 'react-native';
import { authenticateWithDeviceLock } from './deviceAuth';
const AuthScreen = () => {
const handleAuth = async () => {
const result = await authenticateWithDeviceLock();
if (result.success) {
Alert.alert('Success', 'User authenticated');
// 👉 Navigate to Home / Unlock app
} else {
Alert.alert('Failed', result.message);
}
};
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<TouchableOpacity
onPress={handleAuth}
style={{
backgroundColor: '#000',
padding: 15,
borderRadius: 8,
}}
>
<Text style={{ color: '#fff', fontSize: 16 }}>
Unlock with Device Lock
</Text>
</TouchableOpacity>
</View>
);
};
export default AuthScreen;

View File

@@ -0,0 +1,40 @@
import ReactNativeBiometrics from 'react-native-biometrics';
const rnBiometrics = new ReactNativeBiometrics({
allowDeviceCredentials: true, // 👈 IMPORTANT (PIN / Pattern)
});
export const authenticateWithDeviceLock = async () => {
try {
const { available } = await rnBiometrics.isSensorAvailable();
if (!available) {
return {
success: false,
message: 'Device lock is not available',
};
}
const result = await rnBiometrics.simplePrompt({
promptMessage: 'Authenticate to continue',
cancelButtonText: 'Cancel',
});
if (result.success) {
return {
success: true,
message: 'Authentication successful',
};
} else {
return {
success: false,
message: 'Authentication cancelled',
};
}
} catch (error: any) {
return {
success: false,
message: error?.message || 'Authentication failed',
};
}
};