Biometric Unlock
A comprehensive solution for securing your Expo application with biometric authentication (Face ID, Touch ID, Fingerprint) or device passcode. Supports full app lock and verification for specific user actions.
Installation
$ cbui-cli install biometricunlock
cbui-cli
globally and authenticated your account. This will ensure you can access all the necessary features.Import
1import { BiometricProvider, BiometricLock, useBiometric, useBiometricAction, useBiometricSettings } from '@/crossbuildui/biometricunlock'; // Make sure to have expo-local-authentication and expo-secure-store installed.;
expo-local-authentication
and expo-secure-store
. Make sure they are installed in your project.Basic Setup
To enable biometric features, wrap your application's root component with BiometricProvider
and your main content with BiometricLock
. The BiometricProvider
manages the state and configuration, while BiometricLock
renders the lock screen overlay when needed.
1// In your App.tsx or root component
2import { BiometricProvider, BiometricLock } from '@/crossbuildui/biometricunlock';
3import { View, Text } from 'react-native';
4
5const initialConfig = {
6 mode: 'app_lock',
7 lockCondition: 'immediately',
8 allowDevicePasscode: true,
9};
10
11const AppContent = () => {
12 // Your main application content goes here
13 return (
14 <View>
15 <Text>Welcome to the App!</Text>
16 </View>
17 );
18};
19
20export default function App() {
21 return (
22 <BiometricProvider config={initialConfig}>
23 <BiometricLock>
24 <AppContent />
25 </BiometricLock>
26 </BiometricProvider>
27 );
28}
Authentication Modes
The component operates in three modes, configured via the mode
property in the config object:
'app_lock'
: (Default) Secures the entire application. The lock screen is shown on app launch or resume, based on thelockCondition
.'action_verification'
: Does not lock the app automatically. Use theuseBiometricAction
hook to trigger authentication for specific, sensitive user actions.'none'
: Disables all biometric features. The app is never locked, and authentication calls will succeed without a prompt.
1// 1. App Lock Mode (locks the entire app)
2<BiometricProvider config={{ mode: 'app_lock', lockCondition: 'immediately' }}>
3 <BiometricLock>
4 {/* Your App */}
5 </BiometricLock>
6</BiometricProvider>
7
8// 2. Action Verification Mode (for specific actions)
9// Provider config can be 'action_verification' or 'none'
10const MySecureComponent = () => {
11 const { authenticateAction } = useBiometricAction();
12
13 const handleSensitiveAction = async () => {
14 const result = await authenticateAction(async () => {
15 // This part runs only after successful authentication
16 console.log('Action authorized!');
17 return 'Sensitive Data';
18 )
19 };
20
21 if (result.success) {
22 console.log('Received:', result.data);
23 } else {
24 console.error('Authorization failed:', result.error);
25 }
26
27 return <Button onPress={handleSensitiveAction}>Perform Secure Action</Button>;
28}
29
30// 3. None (Biometrics disabled)
31<BiometricProvider config={{ mode: 'none' }}>
32 {/* App content is always visible */}
33</BiometricProvider>
Lock Conditions
In 'app_lock'
mode, you can define when the app should lock after moving to the background using the lockCondition
property:
'immediately'
: Locks the app as soon as it enters the background.'after_delay'
: Locks the app after a specified time. Requires settinglockDelayMinutes
.
1// Lock immediately when app goes to background
2const config1 = {
3 mode: 'app_lock',
4 lockCondition: 'immediately',
5};
6
7// Lock 5 minutes after app goes to background
8const config2 = {
9 mode: 'app_lock',
10 lockCondition: 'after_delay',
11 lockDelayMinutes: 5,
12};
Available Hooks
useBiometric
Accesses the core biometric context. Useful for directly checking lock state or triggering authentication.
useBiometricAction
Provides the authenticateAction
function, ideal for the 'action_verification'
mode to protect specific functions.
useBiometricSettings
A powerful hook for building a settings screen where users can configure their own biometric preferences (e.g., enabling/disabling the lock, changing the lock condition). It provides functions to safely update the global configuration.
1import { useBiometricSettings } from '@/crossbuildui/biometricunlock';
2
3const SettingsScreen = () => {
4 const { settings, toggleAppLock, isAppLockEnabled, updateLockCondition } = useBiometricSettings();
5
6 const handleToggleLock = (enabled) => {
7 toggleAppLock(enabled);
8 if (enabled) {
9 // Optionally set a default lock condition
10 updateLockCondition('immediately');
11 }
12 };
13
14 return (
15 <View>
16 <Text>Enable App Lock</Text>
17 <Switch
18 value={isAppLockEnabled()}
19 onValueChange={handleToggleLock}
20 />
21 {isAppLockEnabled() && (
22 <View>
23 {/* Add more settings controls here, e.g., for lock condition */}
24 </View>
25 )}
26 </View>
27 );
28};
Customizing the Lock Screen
The appearance of the lock screen can be fully customized via the lockScreenConfig
object within your main configuration. You can change text, add a logo, and apply custom styles.
1import { BiometricLock } from '@/crossbuildui/biometricunlock';
2import Icon from 'react-native-vector-icons/MaterialIcons';
3
4const customLockScreenConfig = {
5 appName: 'My Secure App',
6 unlockButtonText: 'Verify to Continue',
7 logo: <Icon name="security" size={48} color="teal" />,
8};
9
10<BiometricProvider config={{ ...initialConfig, lockScreenConfig: customLockScreenConfig }}>
11 <BiometricLock>
12 {/* ... App Content ... */}
13 </BiometricLock>
14</BiometricProvider>
15
Props & Types Overview
BiometricProvider Props
PROP | TYPE | DESCRIPTION |
---|---|---|
children | ReactNode | The child components, typically the entire app. |
config | BiometricUnlockConfig | The initial configuration for biometric locking. |
BiometricLock Props
PROP | TYPE | DESCRIPTION |
---|---|---|
children | ReactNode | Content to display when the app is unlocked. |
config | Partial<BiometricUnlockConfig> | Optional config overrides for this specific lock instance. |
onAuthSuccess | (result: AuthResult) => void | Callback fired on successful authentication. |
onAuthFailed | (error: string) => void | Callback fired on failed authentication. |
onAuthCancelled | () => void | Callback fired when the user cancels the auth prompt. |
onLockStateChange | (isLocked: boolean) => void | Callback fired when the lock state changes. |
isVisible | boolean | Controls the visibility of the component and its lock screen. Default: true . |
style | StyleProp<ViewStyle> | Custom styles for the component container. |
BiometricUnlockConfig Type
KEY | TYPE | DESCRIPTION |
---|---|---|
mode | AuthMode | Authentication mode ('app_lock', 'action_verification', 'none'). |
lockCondition | LockCondition | When to lock the app ('immediately', 'after_delay'). |
lockDelayMinutes | number | Delay in minutes before locking if condition is 'after_delay'. |
autoShowOnAppOpen | boolean | Show auth prompt automatically on app open if locked. |
allowDevicePasscode | boolean | Allow device passcode as a fallback for biometrics. |
promptTitle | string | Custom title for the native authentication prompt. |
lockScreenConfig | LockScreenConfig | Configuration object for the custom lock screen UI. |
Accessibility
The component leverages the native biometric authentication prompts provided by iOS and Android, which are designed to be accessible. The custom lock screen elements are standard React Native components that support accessibility props.