react-native - 循环世博生物识别认证直到成功

标签 react-native expo fingerprint biometrics

我正在尝试使用 React-native 和 Expo 在 Android 上实现生物识别身份验证(faceID/指纹)。

使用 LocalAuthentication.authenticateAsync()功能,用户能够通过他的生物特征进行身份验证。但如果失败,用户必须再次按下生物识别认证。

所以我尝试了一个 recursif 或 do while 循环的小技巧,但结果很奇怪:

const scanFingerPrint = async () => {
        try {
            const results = await DeviceService.biometricAuthentication();
            if (results.success) {
                SecureStoreService.getCredential()
                    .then(credentials => onScan(credentials));
            } else {
                ShakeAnimation(animatedValueModal);
                return scanFingerPrint();
            }
        } catch (e) {
            console.log(e);
        }
    };

使用此代码,如果用户未通过生物识别身份验证,它将无限地传入“else”...

所以我想知道如何在android上处理它。

最佳答案

您可以使用变量手动处理它。
首先创建变量retryCount在构造函数内部或作为类的属性,以便在每个函数中都可以访问它。

constructor(props) {
    super(props);
    this.retryCount = 3; 
}

设置 retryCount 的值调用前 scanFingerPrint功能。
this.retryCount = 3; //number of attempts you want to provide

现在修改如下函数以防止无限循环:
const scanFingerPrint = async () => {
    try {
        if (this.retryCount <= 0){
            //exceeded the number of attempts..try again after a minute
        } else{
            this.retryCount--;
            const results = await DeviceService.biometricAuthentication();
            if (results.success) {
                SecureStoreService.getCredential()
                    .then(credentials => onScan(credentials));
            } else {
                ShakeAnimation(animatedValueModal);
                return scanFingerPrint();
            }
        }
    } catch (e) {
        console.log(e);
    }
};

关于react-native - 循环世博生物识别认证直到成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59422860/

相关文章:

android - "Command failed: ./gradlew installDebug"问题(react-native run-android)

react-native - Picker onValueChange() 调用了两次

javascript - Expo - React Native - Metro 配置文件

regex - 为什么在使用WinSCP .NET程序集时此SSH主机 key 指纹与模式不匹配

java - API指纹Android

javascript - 组合 onLayout 的方法调用

ios - React native 错误窗口导致 iOS 崩溃

javascript对象,如何分配base64类型

react-native - 是否可以在 native react 中使用 react Hook ?

android - 无法加载 exp ://Something went wrong