我正在尝试使用 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/