Expo Firebase 身份验证持久性未按预期工作

Luk*_*ter 5 javascript firebase react-native firebase-authentication expo

我有一个firebaseConfig.js看起来像这样的:

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";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = {...};

export const app = initializeApp(firebaseConfig);

const authState = initializeAuth(app, {
  persistence: getReactNativePersistence(AsyncStorage)
});

export const auth = authState;

export const db = getFirestore(app);
Run Code Online (Sandbox Code Playgroud)

然后,当我登录用户时,它看起来像这样:

import { auth } from "../../firebaseConfig";
...
 signInWithEmailAndPassword(auth, email.trim(), password)
  .then(() => {
     // Handle success.
  })
Run Code Online (Sandbox Code Playgroud)

然后在 App.js 中,我有这个:

import { onAuthStateChanged } from "firebase/auth";
import { auth } from "./firebaseConfig";

... 

 onAuthStateChanged(auth, user => {
  if (user) {
    ...
  } else {
    console.log("No user");
  }
});
Run Code Online (Sandbox Code Playgroud)

每次我刷新应用程序时,我都没有从onAuthStateChanged. 它在登录或注册后成功检测到用户,但在刷新应用程序后却无法检测到。

我究竟做错了什么?

Luk*_*ter 1

该问题已通过使用最新的 firebase 版本(目前为 V10)得到解决。它像您期望的那样处理持久性。