Firebase 中本地持久性错误 - React Native

问题描述 投票:0回答:1

我在 firebase.js 文件中配置 Firebase 持久性以保存在本地,以便我可以在整个应用程序中导出变量,但出现以下错误: FirebaseError: Firebase: Error (auth/already-initialized)., js 引擎:爱马仕

import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { initializeAuth, getReactNativePersistence } from "firebase/auth";
import ReactNativeAsyncStorage from "@react-native-async-storage/async-storage";

export const firebaseConfig = {
  apiKey: "ACHT5u4sf4",
  authDomain: "eacom",
  projectId: "ef",
  storageBucket: "easom",
  messagingSenderId: "93",
  appId: "1:9600e56",
};

export const firebaseapp = initializeApp(firebaseConfig);
export const db = getFirestore(firebaseapp);
export const auth = initializeAuth(firebaseapp, {
  persistence: getReactNativePersistence(ReactNativeAsyncStorage),
});

firebase react-native expo asyncstorage
1个回答
0
投票

错误不断出现,因为您可能尝试使用通过initializeAuth 创建的“auth”。您不需要更改代码中的任何内容,只需添加以下内容:

  1. 从“firebase/auth”导入{getAuth,signInWithEmailAndPassword}
  2. 创建 getAuth 实例: --> const authy = getAuth();
  3. useEffect(() => { signInWithEmailAndPassword(authy, "电子邮件", "密码"); }, [])
  4. useEffect 是可选的,但我将其设置为在应用程序启动时挂载
© www.soinside.com 2019 - 2024. All rights reserved.