Firebase持久性在React Native上不起作用

ffscu2ro  于 2023-08-07  发布在  React
关注(0)|答案(1)|浏览(93)

当我关闭或重新加载我的应用程序时,无论持久性如何,我总是注销。有什么想法吗
我的持久性实现:

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);
    }
  });
};

csbfibhn

csbfibhn1#

我知道我的问题了。getApps.length在设置auth之前返回1,因此它从不选择持久的三进制路由。
我的新代码:

const persistence = getReactNativePersistence(AsyncStorage);
const initializing = !getApps().length;
const FIREBASE_APP = initializing ? initializeApp(firebaseConfig) : getApp();
export const FIREBASE_AUTH = initializing
  ? initializeAuth(FIREBASE_APP, { persistence })
  : getAuth(FIREBASE_APP);

字符串

相关问题