React Native.如何获得用户本地认证每当应用程序来自后台?



嗯,我在网上找不到这样的东西,所以我希望有人能帮我解决这个问题。

我正在创建一个裸反应原生安卓应用程序,我需要检查用户的本地认证每当应用程序来自后台。

我使用Expo Authentication来处理身份验证,然而,用React Native的AppState检查应用状态是没有用的,因为每次身份验证屏幕(生物识别,面部识别甚至PIN号码)出现在屏幕上时,AppState将从活动变为后台,所以它将需要一次又一次的身份验证,因为它总是来自"后台"。

我不能用一个变量来控制它,因为我没有任何标识符可以告诉我应用程序来自博览会本地认证屏幕,所以我被困在这个"哪个先来的,鸡还是蛋"上。问题。

这是目前为止我创建的用来处理它的组件的一部分:

AppState.addEventListener('change', _handleAppStateChange);
return () => {
AppState.removeEventListener('change', _handleAppStateChange);
};
}, []);
const _handleAppStateChange = async (nextAppState: any) => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === 'active'
) {
clearTimeout(authenticationRequiredTimeout);
if (user && shouldAuthenticate) {
LocalAuthentication.hasHardwareAsync().then((hasHardware) => {
if (hasHardware) {
LocalAuthentication.isEnrolledAsync().then((isEnrolled) => {
if (isEnrolled) {
LocalAuthentication.authenticateAsync().then(
(authentication) => {
if (authentication.success !== true) {
logout();
}
setShouldAuthenticate(false);
console.log(authentication);
},
);
}
});
}
});
}
} else {
let timeout = setTimeout(() => {
setShouldAuthenticate(true);
}, 5000);
setAuthenticationRequiredTimeout(timeout);
}
console.log(shouldAuthenticate);
appState.current = nextAppState;
console.log('AppState', appState);
};```
Any help would be much appreciatted

我遇到了同样的问题,并解决如下:

我的应用包含一个store(只是一个类的实例)。在这个商店里我增加了一个属性temporarilyMovedToBackground。每当我调用Expo的localAuthentication时,我首先将属性temporarilyMovedToBackground设置为true。调用结束后,我再次将其设置为false,超时为1000。然后,当检查appState是否更改为背景时,我也检查temporarilyMovedToBackground是否为假。

最新更新