javascript - NextAuth Firebase 后端“ReferenceError:初始化前无法访问‘应用’”
问题描述
我试图在使用下一个身份验证时将用户存储在 firebase 后端我无法解决这个错误
使用 next-auth FIREBASE 适配器。
https://next-auth.js.org/adapters/firebase :DOCS IM FOLLOWING
防火墙客户端
import firebase from "firebase/app";
import "firebase/auth";
import "firebase/firestore";
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASURMENT_ID,
};
// Initialize Firebase
const analytics = getAnalytics(app);
const app = initializeApp(firebaseConfig);
export default firebase;
[...nextauth].js
import NextAuth from "next-auth";
import { FirebaseAdapter } from "@next-auth/firebase-adapter";
import firebaseClient from "../../../firebase/FirebaseClient";
import GoogleProvider from "next-auth/providers/google";
import firebase from "firebase/app";
import "firebase/firestore";
const firestore = (firebase.apps[0] ?? firebaseClient).firestore();
export default NextAuth({
// Configure one or more authentication providers
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
],
adapter: FirebaseAdapter(firestore),
});
呃
ReferenceError: Cannot access 'app' before initialization
解决方案
我敢打赌,您需要按以下方式切换行,因为在创建分析时您没有app
变量:
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
推荐阅读
- regex - 尽管重定向有效,但 Nginx 位置中的匹配返回空 $1
- python - 如何使用 Python 使用 REST API 在 Rally 中解决批量创建的并发冲突
- asp.net-core - 从 ASP.NET Core 2.1 应用程序中删除“服务器”标头
- python-3.x - 为什么它不给我空集?
- javascript - JS互动小说
- c# - 不能使用实例引用访问 C# 静态成员
- javascript - 图像在初始加载时高度较小
- java - 如何在线程结束时处理我当前的 JFrame?
- c# - 如何获取空列表的值类型?
- python - 有没有办法在不安装 Qt 的情况下安装 jupyter?