Firebase用户在App-React Native Expo刷新时注销



上下文

我知道这个问题在stackoverflow已经被问了很多次,相信我,我已经仔细研究过了,并尝试了尽可能多的方法。

问题

我有一个使用Firebase身份验证的React Native-Expo应用程序。用户每次刷新应用程序都会被注销(令人愤怒)。因此,正如许多其他答案所暗示的那样,我添加了一个倾听者。这是添加到我的登录页面上的。在每次刷新之后;没有用户";已登录到我的控制台中。

useEffect(() => {
const unsubscribe = firebase.auth().onAuthStateChanged(user => {
if (user) {
navigation.replace('Main');
console.log('User logged in!');
}
else {
console.log("No user");
}
});
return unsubscribe;
}, []);

当我使用V9.6.10时,我也尝试了模块化方法来回答这里的类似问题。

这些在其他问题上提供的答案似乎对人们有效,但到目前为止还没有一个对我有效。如果需要更多的信息来解决这个问题,我很乐意提供。

这是我开发了9个月的应用程序的最后一期,在一段时间内一直是我的克星,如果能提供帮助,我们将不胜感激!

本文给出了解决方案:使用expo-go 持久化firebase登录的最佳方法

import { initializeApp } from "firebase/app"
import { initializeAuth } from "firebase/auth"
import { getReactNativePersistence } from "firebase/auth/react-native"
import AsyncStorage from "@react-native-async-storage/async-storage"
const defaultApp = initializeApp(config);
initializeAuth(defaultApp, {
persistence: getReactNativePersistence(AsyncStorage)
});