|
| 1 | +--- |
| 2 | +title: Biometrics |
| 3 | +order: 100 |
| 4 | +--- |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +The Biometrics API allows you to authenticate users using their device's biometric sensors like Face ID, Touch ID, or |
| 9 | +fingerprint scanners. |
| 10 | + |
| 11 | +<x-snippet title="Import"> |
| 12 | + |
| 13 | +<x-snippet.tab name="PHP"> |
| 14 | + |
| 15 | +```php |
| 16 | +use Native\Mobile\Facades\Biometrics; |
| 17 | +``` |
| 18 | + |
| 19 | +</x-snippet.tab> |
| 20 | +<x-snippet.tab name="JS"> |
| 21 | + |
| 22 | +```js |
| 23 | +import { biometric, on, off, Events } from '#nativephp'; |
| 24 | +``` |
| 25 | + |
| 26 | +</x-snippet.tab> |
| 27 | +</x-snippet> |
| 28 | + |
| 29 | +## Methods |
| 30 | + |
| 31 | +### `prompt()` |
| 32 | + |
| 33 | +Prompts the user for biometric authentication. |
| 34 | + |
| 35 | +<x-snippet title="Biometric Prompt"> |
| 36 | + |
| 37 | +<x-snippet.tab name="PHP"> |
| 38 | + |
| 39 | +```php |
| 40 | +use Native\Mobile\Facades\Biometrics; |
| 41 | + |
| 42 | +Biometrics::prompt(); |
| 43 | +``` |
| 44 | + |
| 45 | +</x-snippet.tab> |
| 46 | +<x-snippet.tab name="JS"> |
| 47 | + |
| 48 | +```js |
| 49 | +// Basic usage |
| 50 | +await biometric.prompt(); |
| 51 | + |
| 52 | +// With an identifier for tracking |
| 53 | +await biometric.prompt() |
| 54 | + .id('secure-action-auth'); |
| 55 | +``` |
| 56 | + |
| 57 | +</x-snippet.tab> |
| 58 | +</x-snippet> |
| 59 | + |
| 60 | +## Events |
| 61 | + |
| 62 | +### `Completed` |
| 63 | + |
| 64 | +Fired when biometric authentication completes (success or failure). |
| 65 | + |
| 66 | +<x-snippet title="Completed Event"> |
| 67 | + |
| 68 | +<x-snippet.tab name="PHP"> |
| 69 | + |
| 70 | +```php |
| 71 | +use Native\Mobile\Attributes\OnNative; |
| 72 | +use Native\Mobile\Events\Biometric\Completed; |
| 73 | + |
| 74 | +#[OnNative(Completed::class)] |
| 75 | +public function handle(bool $success) |
| 76 | +{ |
| 77 | + if ($success) { |
| 78 | + // User authenticated successfully |
| 79 | + $this->unlockSecureFeature(); |
| 80 | + } else { |
| 81 | + // Authentication failed |
| 82 | + $this->showErrorMessage(); |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +</x-snippet.tab> |
| 88 | +<x-snippet.tab name="Vue"> |
| 89 | + |
| 90 | +```js |
| 91 | +import { biometric, on, off, Events } from '#nativephp'; |
| 92 | +import { ref, onMounted, onUnmounted } from 'vue'; |
| 93 | + |
| 94 | +const isAuthenticated = ref(false); |
| 95 | + |
| 96 | +const handleBiometricComplete = (payload) => { |
| 97 | + if (payload.success) { |
| 98 | + isAuthenticated.value = true; |
| 99 | + unlockSecureFeature(); |
| 100 | + } else { |
| 101 | + showErrorMessage(); |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +const authenticate = async () => { |
| 106 | + await biometric.prompt(); |
| 107 | +}; |
| 108 | + |
| 109 | +onMounted(() => { |
| 110 | + on(Events.Biometric.Completed, handleBiometricComplete); |
| 111 | +}); |
| 112 | + |
| 113 | +onUnmounted(() => { |
| 114 | + off(Events.Biometric.Completed, handleBiometricComplete); |
| 115 | +}); |
| 116 | +``` |
| 117 | + |
| 118 | +</x-snippet.tab> |
| 119 | +<x-snippet.tab name="React"> |
| 120 | + |
| 121 | +```jsx |
| 122 | +import { biometric, on, off, Events } from '#nativephp'; |
| 123 | +import { useState, useEffect } from 'react'; |
| 124 | + |
| 125 | +const [isAuthenticated, setIsAuthenticated] = useState(false); |
| 126 | + |
| 127 | +const handleBiometricComplete = (payload) => { |
| 128 | + if (payload.success) { |
| 129 | + setIsAuthenticated(true); |
| 130 | + unlockSecureFeature(); |
| 131 | + } else { |
| 132 | + showErrorMessage(); |
| 133 | + } |
| 134 | +}; |
| 135 | + |
| 136 | +const authenticate = async () => { |
| 137 | + await biometric.prompt(); |
| 138 | +}; |
| 139 | + |
| 140 | +useEffect(() => { |
| 141 | + on(Events.Biometric.Completed, handleBiometricComplete); |
| 142 | + |
| 143 | + return () => { |
| 144 | + off(Events.Biometric.Completed, handleBiometricComplete); |
| 145 | + }; |
| 146 | +}, []); |
| 147 | +``` |
| 148 | + |
| 149 | +</x-snippet.tab> |
| 150 | +</x-snippet> |
| 151 | + |
| 152 | +## Platform Support |
| 153 | + |
| 154 | +- **iOS:** Face ID, Touch ID |
| 155 | +- **Android:** Fingerprint, Face unlock, other biometric methods |
| 156 | +- **Fallback:** System authentication (PIN, password, pattern) |
| 157 | + |
| 158 | +## Security Notes |
| 159 | + |
| 160 | +- Biometric authentication provides **convenience**, not absolute security |
| 161 | +- Always combine with other authentication factors for sensitive operations |
| 162 | +- Consider implementing session timeouts for unlocked states |
| 163 | +- Users can potentially bypass biometrics if their device is compromised |
0 commit comments