【问题标题】:Loop expo biometric authentication until success循环博览会生物特征认证直到成功
【发布时间】:2020-04-12 19:49:54
【问题描述】:

我正在尝试使用带有 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-native expo fingerprint biometrics


    【解决方案1】:

    Expo 在本地认证的结果上提供一个“错误”键。为了不处理硬件错误,我使用了这个:

    if (!results.success) {
                    switch (results.error) {
                        case "lockout":
                            setLocked(true);
                            break;
                        case "authentication_failed" || "too_fast":
                            ShakeAnimation(animatedValueModal);
                            await scanBiometric();
                            break;
                        case "user_cancel" :
                            break;
                        default:
                            ShakeAnimation(animatedValueModal);
                            break;
                    }
                }
    

    【讨论】:

      【解决方案2】:

      您可以传递函数变量以进行尝试。

      看到这个,

      const scanFingerPrint = async (remainingAttempts = 5) => {  // you can change 5 as per your choice
        try {
          const results = await DeviceService.biometricAuthentication();
          if (results.success) {
            SecureStoreService.getCredential().then(credentials =>
              onScan(credentials)
            );
          } else {
            ShakeAnimation(animatedValueModal);
            if (remainingAttempts) {
              remainingAttempts--;
              scanFingerPrint(remainingAttempts);
            } else {
              alert("You have exceeded max scan limit.");
            }
          }
        } catch (e) {
          console.log(e);
        }
      };
      

      而且您不需要更改任何其他内容。不会触发您的第一次函数调用。

      【讨论】:

        【解决方案3】:

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

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

        在调用scanFingerPrint函数之前设置retryCount的值。

        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);
            }
        };
        

        【讨论】:

          猜你喜欢
          • 2020-04-19
          • 2016-02-18
          • 1970-01-01
          • 1970-01-01
          • 2021-03-23
          • 1970-01-01
          • 2020-09-29
          • 2015-12-21
          • 1970-01-01
          相关资源
          最近更新 更多