Hak*_*ara 7 javascript firebase reactjs firebase-authentication next.js
我正在学习Firebase使用身份验证NextJS,尝试理解使用多个源(文章/youtube),但我遇到了这个错误
ReferenceError: Cannot access 'auth' before initialization
老实说我仍在尝试寻找the source但仍然卡住了
这是我的firebase.js
import { firebase, initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
const firebaseConfig = {
apiKey: process.env.APIKEY,
authDomain: process.env.AUTH,
projectId: process.env.PID,
storageBucket: process.env.BUCKET,
messagingSenderId: process.env.MSID,
appId: process.env.AID,
measurementId: process.env.MID,
};
const app = !firebase.apps.length
? initializeApp(firebaseConfig)
: firebase.app();
const analytics = getAnalytics(app);
const auth = app.auth();
const db = app.firestore();
const googleProvider = new firebase.auth.GoogleAuthProvider();
export {
auth,
db,
signInWithGoogle,
...
};
Run Code Online (Sandbox Code Playgroud)
这是我的login.js页面
import React, { useEffect, useState } from "react";
import {
auth,
signInWithEmailAndPassword,
signInWithGoogle,
} from "../../firebase/index";
import Link from "next/link";
import { useAuthState } from "react-firebase-hooks/auth";
function Login() {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [user, loading, error] = useAuthState(auth);
useEffect(() => {
if (loading) {
// maybe trigger a loading screen
return;
}
if (user) {
alert("GET USER");
console.log(user);
}
}, [user, loading]);
return (
...
Run Code Online (Sandbox Code Playgroud)
我在用着
"firebase": "^9.6.1",
"firebase-admin": "^10.0.1",
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?或者我错过了什么?请帮忙:(
您必须getAuth()从 Firebase Auth SDK 导入,然后初始化它,如下所示:
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getFirestore } from "firebase/firestore";
import { getAnalytics } from "firebase/analytics";
const firebaseConfig = {...};
const app = initializeApp(firebaseConfig)
const analytics = getAnalytics(app);
const auth = getAuth(app);
const db = getFirestore(app);
export {
auth,
db,
};
Run Code Online (Sandbox Code Playgroud)
使用 Modular SDK 时,您无需检查 Firebase 是否已初始化。
| 归档时间: |
|
| 查看次数: |
2001 次 |
| 最近记录: |