react-native - 循环博览会生物特征认证直到成功
问题描述
我正在尝试使用带有 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上处理它。
解决方案
您可以使用变量手动处理它。首先在构造函数中创建变量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);
}
};
推荐阅读
- c++ - 更改派生类对象的字段,但返回后更改已恢复
- python - 如何操作表格
- java - NetBeans IDE 错误:创建 java 项目时出现 java.lang.ClassNotFoundException
- php - Ajax POST 通过使用 XAMPP 的 jQuery 内部服务器错误 500
- r - 在 r 和正则表达式中,如何检测排除某些混合条件的字符
- python - ldap3 中的 ca 证书文件在哪里?
- typescript - 导出默认是忽略?空输出文件
- git - Gitflow - 部署 Repo 子文件夹的最佳实践
- firebase - 函数返回空列表而不是列表
- flutter - 获取异常:setState() 或 markNeedsBuild()