Skip to main content

tauri_plugin_biometric/
lib.rs

1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5//! Prompt the user for biometric authentication.
6//!
7//! - Supported platforms: Android, iOS and macOS.
8
9use tauri::{
10    Manager, Runtime,
11    plugin::{Builder, TauriPlugin},
12};
13
14pub use models::*;
15
16#[cfg(desktop)]
17mod commands;
18#[cfg(desktop)]
19mod desktop;
20mod error;
21#[cfg(mobile)]
22mod mobile;
23mod models;
24
25pub use error::{Error, Result};
26
27#[cfg(desktop)]
28pub use desktop::Biometric;
29#[cfg(mobile)]
30pub use mobile::Biometric;
31
32/// Extensions to [`tauri::App`], [`tauri::AppHandle`], [`tauri::WebviewWindow`], [`tauri::Webview`] and [`tauri::Window`] to access the biometric APIs.
33pub trait BiometricExt<R: Runtime> {
34    /// Returns the [`Biometric`] instance managed by the app.
35    fn biometric(&self) -> &Biometric<R>;
36}
37
38impl<R: Runtime, T: Manager<R>> crate::BiometricExt<R> for T {
39    fn biometric(&self) -> &Biometric<R> {
40        self.state::<Biometric<R>>().inner()
41    }
42}
43
44/// Initializes the plugin.
45pub fn init<R: Runtime>() -> TauriPlugin<R> {
46    let builder = Builder::new("biometric");
47
48    // On mobile the commands are routed directly to the native plugin.
49    #[cfg(desktop)]
50    let builder = builder.invoke_handler(tauri::generate_handler![
51        commands::status,
52        commands::authenticate,
53    ]);
54
55    builder
56        .setup(|app, api| {
57            #[cfg(mobile)]
58            let biometric = mobile::init(app, api)?;
59            #[cfg(desktop)]
60            let biometric = desktop::init(app, api)?;
61            app.manage(biometric);
62            Ok(())
63        })
64        .build()
65}