React Native中Firebase Auth持久化失效问题求助
React Native中Firebase Auth持久化失效问题求助
大家好,我碰到一个特别头疼的问题:不管怎么配置Firebase Auth的持久化,每次关闭或者重载我的React Native应用时,用户都会被自动登出,有没有朋友能给点排查思路或者解决方案?
我的持久化实现代码如下:
import { getReactNativePersistence } from "firebase/auth/react-native"; import { initializeApp, getApps, getApp } from "firebase/app"; import { initializeAuth, getAuth, onAuthStateChanged, } from "firebase/auth"; const persistence = getReactNativePersistence(AsyncStorage); const FIREBASE_APP = !getApps().length ? initializeApp(firebaseConfig) : getApp(); export const FIREBASE_AUTH = !getApps().length ? initializeAuth(FIREBASE_APP, { persistence }) : getAuth(FIREBASE_APP);
我用来监听认证状态变化的代码:
export const authObserver = () => { onAuthStateChanged(FIREBASE_AUTH, async (authUser) => { try { if (authUser) { console.log("User logged in"); processUser(authUser); } else { console.log("Not logged in"); // 每次重载应用后都会走到这个分支 } } catch (err) { clearStorage("authUser"); clearStorage("user"); console.log("ERR:", err); } }); };
备注:内容来源于stack exchange,提问作者Adrian Bartholomew




