当我关闭或重新加载我的应用程序时,无论持久性如何,我总是注销。有什么想法吗
我的持久性实现:
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"); // Always goes here after reload.
}
} catch (err) {
clearStorage("authUser");
clearStorage("user");
console.log("ERR:", err);
}
});
};
型
1条答案
按热度按时间csbfibhn1#
我知道我的问题了。
getApps.length
在设置auth之前返回1,因此它从不选择持久的三进制路由。我的新代码:
字符串