Getting Started
Konten ini belum tersedia dalam bahasa Anda.
-
Install the package
Terminal window npm i @capgo/capacitor-native-biometricTerminal window pnpm add @capgo/capacitor-native-biometricTerminal window yarn add @capgo/capacitor-native-biometricTerminal window bun add @capgo/capacitor-native-biometric -
Sync with native projects
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync -
Configure permissions
iOS
Add Face ID usage description to your
Info.plist
:<key>NSFaceIDUsageDescription</key><string>For secure authentication</string>Android
Add biometric permission to your
AndroidManifest.xml
:<uses-permission android:name="android.permission.USE_BIOMETRIC" />
Usage
Import the plugin and use its methods for biometric authentication:
import { NativeBiometric } from '@capgo/capacitor-native-biometric';
// Check if biometric authentication is availableconst checkBiometric = async () => { const result = await NativeBiometric.isAvailable();
if (result.isAvailable) { console.log(`Biometric type: ${result.biometryType}`); }};
// Perform biometric verificationconst verify = async () => { const verified = await NativeBiometric.verifyIdentity({ reason: "For secure access to your account", title: "Authentication", subtitle: "Verify your identity", description: "Place your finger on the sensor" }) .then(() => true) .catch(() => false);
if (verified) { console.log("Authentication successful"); }};
// Store credentials securelyconst saveCredentials = async () => { await NativeBiometric.setCredentials({ username: "user@example.com", password: "securepassword", server: "https://api.example.com" });};
// Retrieve stored credentialsconst getCredentials = async () => { const credentials = await NativeBiometric.getCredentials({ server: "https://api.example.com" });
console.log(credentials.username); console.log(credentials.password);};
// Delete stored credentialsconst deleteCredentials = async () => { await NativeBiometric.deleteCredentials({ server: "https://api.example.com" });};
API Reference
isAvailable()
Checks if biometric authentication is available on the device.
interface AvailableResult { isAvailable: boolean; biometryType?: BiometryType; errorCode?: number;}
enum BiometryType { NONE = 0, TOUCH_ID = 1, FACE_ID = 2, FINGERPRINT = 3, FACE_AUTHENTICATION = 4, IRIS_AUTHENTICATION = 5}
verifyIdentity(options)
Performs biometric authentication.
interface BiometricOptions { reason?: string; title?: string; subtitle?: string; description?: string; fallbackTitle?: string; useFallback?: boolean; maxAttempts?: number;}
setCredentials(options)
Stores credentials securely with biometric protection.
interface Credentials { username: string; password: string; server: string;}
getCredentials(options)
Retrieves stored credentials after biometric authentication.
interface GetCredentialOptions { server: string;}
deleteCredentials(options)
Deletes stored credentials.
interface DeleteCredentialOptions { server: string;}
Best Practices
-
Always check availability first
const result = await NativeBiometric.isAvailable();if (!result.isAvailable) {// Fall back to password authentication} -
Provide clear reasons Always explain why you need biometric authentication to build user trust.
-
Handle errors gracefully
try {await NativeBiometric.verifyIdentity({reason: "Access your secure data"});} catch (error) {// Handle cancellation or failureconsole.log("Authentication failed or cancelled");} -
Use appropriate fallbacks Enable passcode fallback for devices where biometrics may fail.
Platform Differences
iOS
- Supports Touch ID and Face ID
- Requires
NSFaceIDUsageDescription
for Face ID - Fallback to device passcode available
Android
- Supports fingerprint, face, and iris recognition
- Requires
USE_BIOMETRIC
permission - Biometric Prompt API for Android 9+