我正在 React 中构建一个项目,它具有身份验证,所以我正在使用 firebase
我遇到过这个错误
Firebase: Need to provide options, when not being deployed to hosting via source. (app/no-options)
这是我的 config-firebase 文件:
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyBoKEW_g0gOwNKkHGMAyhXxC0ESfdsVhKI",
authDomain: "kargoevi-auth.firebaseapp.com",
projectId: "kargoevi-auth",
storageBucket: "kargoevi-auth.appspot.com",
messagingSenderId: "726037811463",
appId: "1:726037811463:web:42d75c7f5c1d1b5b9bf5a2",
measurementId: "G-PJXGLVZ6GQ",
};
export const auth = getAuth(app);
const app = initializeApp(firebaseConfig);
export default app;
这是我的授权文件:
import React, { useState, useEffect } from "react";
import { onAuthStateChanged } from "firebase/auth";
import { auth } from "./base";
export const AuthContext = React.createContext();
export const AuthProvider = ({ children }) => {
const [currentUser, setCurrentUser] = useState(null);
const [pending, setPending] = useState(true);
useEffect(() => {
onAuthStateChanged(auth, (user) => {
setCurrentUser(user);
setPending(false);
});
}, []);
if (pending) {
return <>Loading...</>;
}
return (
<AuthContext.Provider
value={{
currentUser,
}}
>
{children}
</AuthContext.Provider>
);
};
注意:我还没有尝试部署它
关于如何修复此错误的任何想法?
最佳答案
在使用任何其他 Firebase 服务之前,必须先初始化 Firebase 应用,因此请确保首先调用 initializeApp()
函数。
// correct order
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
最好在其他文件中使用导出的auth
实例。如果您也在其他文件中使用 getAuth()
,则 initializeApp()
可能尚未被调用,但会导致同样的错误。
关于javascript - 未通过源部署到托管时如何提供选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74016589/