我正在 React 中構建一個具有身份驗證的專案,所以我使用的是 firebase
我遇到了這個錯誤
Firebase:當不通過源部署到托管時,需要提供選項。(應用程式/無選項)
這是我的 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>
);
};
注意:我還沒有嘗試部署它
關于如何解決此錯誤的任何想法?
uj5u.com熱心網友回復:
在您使用任何其他 Firebase 服務之前,必須先初始化 Firebase 應用程式,因此請確保initializeApp()首先呼叫該函式。
// correct order
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
最好auth在其他檔案中使用匯出的實體。如果您getAuth()也在其他檔案中使用,則有可能initializeApp()尚未呼叫但導致相同的錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/513157.html
標籤:Google Cloud Collective javascript反应火力基地
上一篇:將函式輸出回傳到反應js中的組件
