|
1 | 1 | --- |
2 | 2 | title: Biometric |
3 | 3 | description: Prompt the user for biometric authentication on Android and iOS. |
4 | | -sidebar: |
5 | | - badge: |
6 | | - text: WIP |
7 | | - variant: caution |
8 | 4 | --- |
9 | 5 |
|
10 | | -import Stub from '@components/Stub.astro'; |
11 | 6 | import PluginLinks from '@components/PluginLinks.astro'; |
| 7 | +import { Tabs, TabItem, Steps } from '@astrojs/starlight/components'; |
| 8 | +import CommandTabs from '@components/CommandTabs.astro'; |
12 | 9 | import PluginPermissions from '@components/PluginPermissions.astro'; |
13 | 10 |
|
14 | 11 | <PluginLinks plugin="biometric" /> |
15 | 12 |
|
16 | | -<Stub> |
| 13 | +Prompt the user for biometric authentication on Android and iOS. |
17 | 14 |
|
18 | | -Based on https://github.com/tauri-apps/plugins-workspace/tree/plugins/biometric |
| 15 | +## Supported Platforms |
19 | 16 |
|
20 | | -</Stub> |
| 17 | +- Android |
| 18 | +- iOS |
| 19 | + |
| 20 | +## Setup |
| 21 | + |
| 22 | +Install the biometric plugin to get started. |
| 23 | + |
| 24 | +<Tabs> |
| 25 | + <TabItem label="Automatic"> |
| 26 | + |
| 27 | + Use your project's package manager to add the dependency: |
| 28 | + |
| 29 | + <CommandTabs npm="npm run tauri add biometric" |
| 30 | + yarn="yarn run tauri add biometric" |
| 31 | + pnpm="pnpm tauri add biometric" |
| 32 | + bun="bun tauri add biometric" |
| 33 | + cargo="cargo tauri add biometric" /> |
| 34 | + |
| 35 | + </TabItem> |
| 36 | + <TabItem label="Manual"> |
| 37 | + <Steps> |
| 38 | + 1. Install the biometric plugin by adding the following to your `Cargo.toml` file: |
| 39 | + |
| 40 | + ```toml title="src-tauri/Cargo.toml" |
| 41 | + [dependencies] |
| 42 | + tauri-plugin-biometric = "2.0.0-beta" |
| 43 | + # alternatively with Git: |
| 44 | + tauri-plugin-biometric = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } |
| 45 | + ``` |
| 46 | + |
| 47 | + 2. Modify `lib.rs` to initialize the plugin: |
| 48 | + |
| 49 | + ```rust title="src-tauri/src/lib.rs" ins={4} |
| 50 | + #[cfg_attr(mobile, tauri::mobile_entry_point)] |
| 51 | + fn run() { |
| 52 | + tauri::Builder::default() |
| 53 | + .plugin(tauri_plugin_biometric::Builder::new().build()) |
| 54 | + .run(tauri::generate_context!()) |
| 55 | + .expect("error while running tauri application"); |
| 56 | + } |
| 57 | + ``` |
| 58 | + |
| 59 | + 3. Install the JavaScript Guest bindings using your preferred JavaScript package manager: |
| 60 | + |
| 61 | + <CommandTabs |
| 62 | + npm = "npm install @tauri-apps/plugin-biometric" |
| 63 | + yarn = "yarn add @tauri-apps/plugin-biometric" |
| 64 | + pnpm = "pnpm add @tauri-apps/plugin-biometric" |
| 65 | + bun = "bun add @tauri-apps/plugin-biometric" |
| 66 | + /> |
| 67 | + </Steps> |
| 68 | + </TabItem> |
| 69 | + |
| 70 | +</Tabs> |
| 71 | + |
| 72 | +## Usage |
| 73 | + |
| 74 | +This plugin enables you to verify the availability of Biometric Authentication on a device, prompt the user for biometric authentication, and check the result to determine if the authentication was successful or not. |
| 75 | + |
| 76 | +### Check Status |
| 77 | + |
| 78 | +You can check the status of Biometric Authentication, including its availability and the types of biometric authentication methods supported. |
| 79 | + |
| 80 | +<Tabs> |
| 81 | +<TabItem label="JavaScript"> |
| 82 | + |
| 83 | +```javascript |
| 84 | +import { checkStatus } from '@tauri-apps/plugin-biometric'; |
| 85 | + |
| 86 | +const status = await checkStatus(); |
| 87 | +if (status.isAvailable) { |
| 88 | + console.log('Yes! Biometric Authentication is available'); |
| 89 | +} else { |
| 90 | + console.log( |
| 91 | + 'No! Biometric Authentication is not available due to ' + status.error |
| 92 | + ); |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +</TabItem> |
| 97 | +<TabItem label="Rust"> |
| 98 | + |
| 99 | +```rust |
| 100 | +use tauri_plugin_biometric::BiometricExt; |
| 101 | + |
| 102 | +fn check_biometric(app_handle: tauri::AppHandle) { |
| 103 | + let status = app_handle.biometric().status().unwrap(); |
| 104 | + if status.is_available { |
| 105 | + println!("Yes! Biometric Authentication is available"); |
| 106 | + } else { |
| 107 | + println!("No! Biometric Authentication is not available due to: {}", status.error.unwrap()); |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +</TabItem> |
| 113 | +</Tabs> |
| 114 | + |
| 115 | +### Authenticate |
| 116 | + |
| 117 | +To prompt the user for Biometric Authentication, utilize the `authenticate()` method. |
| 118 | + |
| 119 | +<Tabs> |
| 120 | + |
| 121 | +<TabItem label="JavaScript"> |
| 122 | + |
| 123 | +```javascript ins={18} |
| 124 | +import { authenticate } from '@tauri-apps/plugin-biometric'; |
| 125 | + |
| 126 | +const options = { |
| 127 | + // Set true if you want the user to be able to authenticate using phone password |
| 128 | + allowDeviceCredential: false, |
| 129 | + cancelTitle: "Feature won't work if Canceled", |
| 130 | + |
| 131 | + // iOS only feature |
| 132 | + fallbackTitle: 'Sorry, authentication failed', |
| 133 | + |
| 134 | + // Android only features |
| 135 | + title: 'Tauri feature', |
| 136 | + subtitle: 'Authenticate to access the locked Tauri function', |
| 137 | + confirmationRequired: true, |
| 138 | +}; |
| 139 | + |
| 140 | +try { |
| 141 | + await authenticate('This feature is locked', options); |
| 142 | + console.log( |
| 143 | + 'Hooray! Successfully Authenticated! We can now perform the locked Tauri function!' |
| 144 | + ); |
| 145 | +} catch (err) { |
| 146 | + console.log('Oh no! Authentication failed because ' + err.message); |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +</TabItem> |
| 151 | + |
| 152 | +<TabItem label="Rust"> |
| 153 | + |
| 154 | +```rust ins={21} |
| 155 | +use tauri_plugin_biometric::{BiometricExt, AuthOptions}; |
| 156 | + |
| 157 | +fn bio_auth(app_handle: tauri::AppHandle) { |
| 158 | + |
| 159 | + let options = AuthOptions { |
| 160 | + // Set True if you want the user to be able to authenticate using phone password |
| 161 | + allow_device_credential:false, |
| 162 | + cancel_title: Some("Feature won't work if Canceled".to_string()), |
| 163 | + |
| 164 | + // iOS only feature |
| 165 | + fallback_title: Some("Sorry, authentication failed".to_string()), |
| 166 | + |
| 167 | + // Android only features |
| 168 | + title: Some("Tauri feature".to_string()), |
| 169 | + subtitle: Some("Authenticate to access the locked Tauri function".to_string()), |
| 170 | + confirmation_required: Some(true), |
| 171 | + }; |
| 172 | + |
| 173 | + // if the authentication was succesfull, the function returns Result::Ok() |
| 174 | + // otherwise returns Result::Error() |
| 175 | + match app_handle.biometric().authenticate("This feature is locked".to_string(), options) { |
| 176 | + Ok(_) => { |
| 177 | + println!("Hooray! Successfully Authenticated! We can now perform the locked Tauri function!"); |
| 178 | + } |
| 179 | + Err(e) => { |
| 180 | + println!("Oh no! Authentication failed because : {e}"); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | +``` |
| 185 | + |
| 186 | +</TabItem> |
| 187 | +</Tabs> |
| 188 | + |
| 189 | +## Permissions |
| 190 | + |
| 191 | +By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your `capabilities` configuration to enable these. |
| 192 | + |
| 193 | +See the [Capabilities Overview](/security/capabilities/) for more information and the [step by step guide](/learn/security/using-plugin-permissions/) to use plugin permissions. |
| 194 | + |
| 195 | +```json title="src-tauri/capabilities/main.json" ins={6} |
| 196 | +{ |
| 197 | + "$schema": "../gen/schemas/desktop-schema.json", |
| 198 | + "identifier": "main-capability", |
| 199 | + "description": "Capability for the main window", |
| 200 | + "windows": ["main"], |
| 201 | + "permissions": ["biometric:default"] |
| 202 | +} |
| 203 | +``` |
21 | 204 |
|
22 | 205 | <PluginPermissions plugin="biometric" /> |
0 commit comments