我尝试在我的React.JS应用程序上实现Firebase进行身份验证。然而,我一直得到这个错误
ERROR in ./src/Pages/UserPage/contexts/AuthContext.js 8:0-32
模块未找到:错误:包路径。没有从package/Users/Aarnav/Desktop/JavaScript/my-app copy 2/node_modules/firebase中导出(参见/Users/Aarnav/Desktop/JavaScript/my-app copy 2/node_modules/firebase/package.json中的exports字段)
我的firebase设置是这样的,
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
const firebaseConfig = {
apiKey: "AIzaSyCPSbjZ9F1TgI43T0SH68rUi9TMHBXRw - s",
authDomain: "csia-41409.firebaseapp.com",
databaseURL: "https://csia-41409-default-rtdb.firebaseio.com",
projectId: "csia-41409",
storageBucket: "csia-41409.appspot.com",
messagingSenderId: "714268776254",
appId: "1:714268776254:web:93d9ef26a5d9c7d7f8be6f",
};
const app = initializeApp(firebaseConfig);
export const auth = getAuth();
和我的authcontext看起来
import React, { useContext, useState, useEffect } from "react"
import { auth } from "firebase";
const AuthContext = React.createContext();
export function useAuth() {
return useContext(AuthContext);
}
export function AuthProvider({ children }) {
const [currentUser, setCurrentUser] = useState();
const [loading, setLoading] = useState(true);
function signup(email, password) {
return auth.createUserWithEmailAndPassword(email, password);
}
function login(email, password) {
return auth.signInWithEmailAndPassword(email, password);
}
useEffect(() => {
const unsubscribe = auth.onAuthStateChanged((user) => {
setCurrentUser(user);
setLoading(false);
});
return unsubscribe;
}, []);
auth.onAuthStateChanged((user) => {
setCurrentUser(user);
});
const value = {
currentUser,
login,
signup,
};
return (
<AuthContext.Provider value={value}>
{!loading && children}
</AuthContext.Provider>
);
}
也因为某些原因,我的整个网站是一个空白的屏幕,无论我在哪个路由器(页面)上。我相信这是因为上面提到的错误,但我不确定。
起初我认为这是因为我使用的是firebase 9.14版本,然而,我更新了我的firebase设置,但这是无济于事的,错误仍然存在。我不知道如何删除这个错误。
在firebase V9中,您应该以这种方式使用电子邮件和密码进行注册和登录:
import { createUserWithEmailAndPassword, getAuth, signInWithEmailAndPassword } from "firebase/auth";
const auth = getAuth();
function signup(email, password) {
return createUserWithEmailAndPassword(auth, email, password);
}
function login(email, password) {
return signInWithEmailAndPassword(auth, email, password);
}