React Native Firebase undefined 不是一个对象(评估 'this.storage.setItem')

React Native Firebase undefined is not an object (evaluating 'this.storage.setItem')

这是我第一次使用 firebase,我正在尝试设置持久性但出现错误。我是 在 react native 上使用 expo。这是我的代码:

import firebase from "firebase/compat/app";
import "firebase/compat/auth";
import "firebase/compat/firestore";

const confirmCode = async () => {
        const credential = firebase.auth.PhoneAuthProvider.credential(
          verificationId,
          code
        );
    
        firebase
          .auth()
          .setPersistence(firebase.auth.Auth.Persistence.LOCAL)
          .then(async () => {
            return firebase
              .auth()
              .signInWithCredential(credential)
              .then((result) => {
                setIsNew(result.additionalUserInfo.isNewUser);
                result.user.getIdToken().then((token) => setUserToken(token));
              });
          })
          .catch((error) => {
            console.log(error.message);
          });
      };

我遇到错误 undefined is not an object (evaluating 'this.storage.setItem') 当谈到持久化时。

我正在使用 google 文档中的 this 页面和最新版本的 firebase 9.6.8。它在没有持久性的情况下工作正常,但用户每次打开应用程序时都必须登录。

我在 Whosebug 和 github 上搜索了数十个主题,但我没有找到该问题的任何答案。

而且这也没有显示任何用户在有或没有坚持的情况下登录:

if (firebase.auth().currentUser !== null) {
    console.log(firebase.auth().currentUser.uid);
  } else {
    console.log("no users logged");
  }

我想过以某种方式使用异步存储,但我真的不知道如何让它发挥作用。

好的,所以我意识到要保留登录状态,如果使用 expo,您实际上必须执行一个特殊的过程。您需要初始化自己的身份验证,传入异步存储,就像这样。这样做时,不需要设置持久化。

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

如果有人遇到同样的问题 - 我在 Bernard Allotey 的帮助下设法解决了它。这是我的做法:

在App.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 firebaseConfig from "./firebaseConfing";
    
    useEffect(async () => {
        const defaultApp = initializeApp(firebaseConfig);
        initializeAuth(defaultApp, {
          persistence: getReactNativePersistence(AsyncStorage),
        });

     }, []);

在我的第一个屏幕中:

import { getAuth, onAuthStateChanged } from "firebase/auth";

onAuthStateChanged(getAuth(), (user) => {
    if (user) {
      console.log(user);
    } else {
      console.log("signed out");
    }
  });

对于登录,我使用的是:

    import { getAuth, signInWithPhoneNumber } from "firebase/auth";
    
    async function sendVerification() {
        const auth = getAuth();
        signInWithPhoneNumber(
          auth,
          countryCode + phoneNumber,
          recaptchaVerifier.current
        ).then((confirmationResult) =>
          navigation.navigate("SmsCode", {
            confirmationResult: confirmationResult,
            phoneNumber: phoneNumber,
          })
        );
      }
    
// next screen

    const confirmCode = async () => {
        confirmationResult.confirm(code).then((result) => {
          result.user.getIdToken().then(async (token) => {
            setUserToken(token);
          });
        });
      };

现在用户如我所愿的坚持了。非常感谢您的帮助。