[英]FIREBASE - ReferenceError: Cannot access 'auth' before initialization
[英]NextAuth Firebase Backend "ReferenceError: Cannot access 'app' before initialization"
我試圖在使用 next auth 時將用戶存儲在 firebase 后端 我無法解決此錯誤
使用 next-auth FIREBASE 適配器。
https://next-auth.js.org/adapters/firebase :我正在關注文檔
FIREBASE 客戶
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);
我們的問題有點不同。 如果您開發了一個雲 function,它有一個內部引用的 function,如下所示:
export.foo = functions.https.onCall(async () => {
// Some functionality here that references internalFunc
const internalFunc = () => { ... }
}
該internalFunc
將無法被引用。
我們的解決方案是在我們的函數目錄中創建一個utilities.js
文件,然后從該文件中導出internalFunc
。 然后,將該 function 導入到部署雲 function 的腳本中。
希望這對某人有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.