首页 > 解决方案 > 循环博览会生物特征认证直到成功

问题描述

我正在尝试使用带有 Expo 的 React-native 在 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上处理它。

标签: react-nativeexpofingerprintbiometrics

解决方案


您可以使用变量手动处理它。首先在构造函数中创建变量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);
    }
};

推荐阅读