Prompt the user for biometric authentication on Android and iOS.
This plugin requires a Rust version of at least 1.77.2
Platform Level Notes windows
linux
macos
android
ios
Install the biometric plugin to get started.
Use your project’s package manager to add the dependency:
npm run tauri add biometric
yarn run tauri add biometric
deno task tauri add biometric
cargo tauri add biometric
Run the following command in the src-tauri
folder to add the plugin to the project’s dependencies in Cargo.toml
:
cargo add tauri-plugin-biometric --target ' cfg(any(target_os = "android", target_os = "ios")) '
Modify lib.rs
to initialize the plugin:
#[cfg_attr(mobile, tauri :: mobile_entry_point)]
tauri :: Builder :: default ()
app . handle () . plugin (tauri_plugin_biometric :: Builder :: new () . build ());
. run (tauri :: generate_context! ())
. expect ( " error while running tauri application " );
Install the JavaScript Guest bindings using your preferred JavaScript package manager:
npm install @tauri-apps/plugin-biometric
yarn add @tauri-apps/plugin-biometric
pnpm add @tauri-apps/plugin-biometric
deno add npm:@tauri-apps/plugin-biometric
bun add @tauri-apps/plugin-biometric
On iOS the biometric plugin requires the NSFaceIDUsageDescription
information property list value, which should describe why your app needs to use biometric authentication.
In the src-tauri/Info.ios.plist
file, add the following snippet:
<? xml version = " 1.0 " encoding = " UTF-8 " ?>
<! DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
< key > NSFaceIDUsageDescription </ key >
< string > Authenticate with biometric </ string >
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.
You can check the status of Biometric Authentication, including its availability and the types of biometric authentication methods supported.
import { checkStatus } from ' @tauri-apps/plugin-biometric ' ;
const status = await checkStatus ();
if ( status . isAvailable ) {
console . log ( ' Yes! Biometric Authentication is available ' );
' No! Biometric Authentication is not available due to ' + status . error
use tauri_plugin_biometric :: BiometricExt;
fn check_biometric ( app_handle : tauri :: AppHandle) {
let status = app_handle . biometric () . status () . unwrap ();
println! ( " Yes! Biometric Authentication is available " );
println! ( " No! Biometric Authentication is not available due to: {} " , status . error . unwrap ());
To prompt the user for Biometric Authentication, utilize the authenticate()
method.
import { authenticate } from ' @tauri-apps/plugin-biometric ' ;
// Set true if you want the user to be able to authenticate using phone password
allowDeviceCredential: false ,
cancelTitle: " Feature won't work if Canceled " ,
fallbackTitle: ' Sorry, authentication failed ' ,
subtitle: ' Authenticate to access the locked Tauri function ' ,
confirmationRequired: true ,
await authenticate ( ' This feature is locked ' , options );
' Hooray! Successfully Authenticated! We can now perform the locked Tauri function! '
console . log ( ' Oh no! Authentication failed because ' + err . message );
use tauri_plugin_biometric :: {BiometricExt, AuthOptions};
fn bio_auth ( app_handle : tauri :: AppHandle) {
let options = AuthOptions {
// Set True if you want the user to be able to authenticate using phone password
allow_device_credential : false ,
cancel_title : Some( " Feature won't work if Canceled " . to_string ()),
fallback_title : Some( " Sorry, authentication failed " . to_string ()),
title : Some( " Tauri feature " . to_string ()),
subtitle : Some( " Authenticate to access the locked Tauri function " . to_string ()),
confirmation_required : Some( true ),
// if the authentication was successful, the function returns Result::Ok()
// otherwise returns Result::Error()
match app_handle . biometric () . authenticate ( " This feature is locked " . to_string (), options ) {
println! ( " Hooray! Successfully Authenticated! We can now perform the locked Tauri function! " );
println! ( " Oh no! Authentication failed because : {e} " );
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.
See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
"$schema" : " ../gen/schemas/desktop-schema.json " ,
"identifier" : " main-capability " ,
"description" : " Capability for the main window " ,
"permissions" : [ " biometric:default " ]
This permission set configures which
biometric features are by default exposed.
It allows acccess to all biometric commands.
allow-authenticate
allow-status
Permission Table
Identifier
Description
biometric:allow-authenticate
Enables the authenticate command without any pre-configured scope.
biometric:deny-authenticate
Denies the authenticate command without any pre-configured scope.
biometric:allow-status
Enables the status command without any pre-configured scope.
biometric:deny-status
Denies the status command without any pre-configured scope.
© 2024 Tauri Contributors. CC-BY / MIT