Skip to content

生物识别

付费插件

此插件需要购买。价格:$49(一次性购买)

概述

Biometrics API 允许你使用设备的生物识别传感器(如 Face ID、Touch ID 或指纹扫描器)对用户进行身份验证。

安装

shell
composer require nativephp/mobile-biometrics

使用

PHP (Livewire/Blade)

php
use Native\Mobile\Facades\Biometrics;

Biometrics::prompt();

JavaScript (Vue/React/Inertia)

js
import { biometric, on, off, Events } from '#nativephp';

// 基本用法
await biometric.prompt();

// 带有用于跟踪的标识符
await biometric.prompt()
    .id('secure-action-auth');

事件

Completed

当生物识别认证完成(成功或失败)时触发。

php
use Native\Mobile\Attributes\OnNative;
use Native\Mobile\Events\Biometric\Completed;

#[OnNative(Completed::class)]
public function handle(bool $success)
{
    if ($success) {
        // 用户认证成功
        $this->unlockSecureFeature();
    } else {
        // 认证失败
        $this->showErrorMessage();
    }
}
js
import { biometric, on, off, Events } from '#nativephp';
import { ref, onMounted, onUnmounted } from 'vue';

const isAuthenticated = ref(false);

const handleBiometricComplete = (payload) => {
    if (payload.success) {
        isAuthenticated.value = true;
        unlockSecureFeature();
    } else {
        showErrorMessage();
    }
};

const authenticate = async () => {
    await biometric.prompt();
};

onMounted(() => {
    on(Events.Biometric.Completed, handleBiometricComplete);
});

onUnmounted(() => {
    off(Events.Biometric.Completed, handleBiometricComplete);
});
jsx
import { biometric, on, off, Events } from '#nativephp';
import { useState, useEffect } from 'react';

const [isAuthenticated, setIsAuthenticated] = useState(false);

const handleBiometricComplete = (payload) => {
    if (payload.success) {
        setIsAuthenticated(true);
        unlockSecureFeature();
    } else {
        showErrorMessage();
    }
};

const authenticate = async () => {
    await biometric.prompt();
};

useEffect(() => {
    on(Events.Biometric.Completed, handleBiometricComplete);

    return () => {
        off(Events.Biometric.Completed, handleBiometricComplete);
    };
}, []);

平台支持

  • iOS: Face ID、Touch ID
  • Android: 指纹、面部解锁、其他生物识别方法
  • 回退: 系统认证(PIN、密码、图案)

安全注意事项

  • 生物识别认证提供的是便利性,而非绝对安全
  • 对于敏感操作,始终与其他认证因素结合使用
  • 考虑为解锁状态实现会话超时
  • 如果用户设备被入侵,用户可能会绕过生物识别

基于 NativePHP 官方文档翻译