javascript 使用firebase适配器的Next Auth验证在Next JS 13上不工作

iezvtpos  于 2023-01-16  发布在  Java
关注(0)|答案(1)|浏览(116)

我尝试使用Next Auth和firebase为我的Next JS 13应用程序实现一个Auth功能。但是当我添加所有内容并运行代码时,它抛出了一个错误
错误-Firebase错误:Firebase:名为“[DEFAULT]”的Firebase应用程序已存在,但具有不同的选项或配置(应用程序/重复应用程序)。
当我直接使用firbaseconfig到适配器时,它可以工作。但是当我从firebase.js文件导入它时,它会抛出上述错误。
firebase.js

import { initializeApp, getApp, getApps } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { getStorage } from "firebase/storage";

const firebaseConfig = {
  apiKey: "AIzaSyABiIZoymM13sZzAbzDThOgYHyB-MDm4aY",
  authDomain: "sanahproperty.firebaseapp.com",
  projectId: "sanahproperty",
  storageBucket: "sanahproperty.appspot.com",
  messagingSenderId: "543489729763",
  appId: "1:543489729763:web:aa00165176675e2b9c51d6",
};

const app = getApps.length > 0 ? getApp() : initializeApp(firebaseConfig);

const db = getFirestore(app);
const storage = getStorage(app);

export { db, storage, app };

[...nextauth].js

import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import { FirestoreAdapter } from "@next-auth/firebase-adapter";
import { db } from "../../../firebase";

export default NextAuth({
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_ID,
      clientSecret: process.env.GOOGLE_SECRET,
    }),
  ],
  adapter: FirestoreAdapter(db),
  // ...
});

帮帮我吧,自从我升级到下一代JS 13后,这里就像地狱一样。
仅在firebase.js中更新

const firebaseConfig = {
  apiKey: "AIzaSyABiIZoymM13sZzAbzDThOgYHyB-MDm4aY",
  authDomain: "sanahproperty.firebaseapp.com",
  projectId: "sanahproperty",
  storageBucket: "sanahproperty.appspot.com",
  messagingSenderId: "543489729763",
  appId: "1:543489729763:web:aa00165176675e2b9c51d6",
};

export const app =
  getApps().length > 0 ? getApp() : initializeApp(firebaseConfig);

export const db = getFirestore(app);
export const storage = getStorage(app);

错误指向'[... nextauth].js中的此位置
adapter: FirestoreAdapter(db),指向数据库条目

yqkkidmi

yqkkidmi1#

getApps是一个函数,但您正在使用.length而没有调用它。请尝试:

const app = getApps().length > 0 ? getApp() : initializeApp(firebaseConfig);

// getApps.length    => 0 ✕
// getApps().length  => 1 ✔️

相关问题