FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created #526
-
I'm getting this error in my This is my and this is my Any idea of what is happening? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
I just needed to add |
Beta Was this translation helpful? Give feedback.
-
I think that something is not working properly. When I try to access the auth object from firebase/auth in my login.tsx it seems to not be initialized yet so that's why it's crashing. How can I wait until the initAuth() in executed before calling the getAuth() from Firebase SDK? |
Beta Was this translation helpful? Give feedback.
-
I solved this by using React Contexts. I have not built my app completely so I'm not sure how it will work later with queries and such, but using this code you can access what you need from firebase in your react components FirebaseContext.tsx import { FirebaseApp, initializeApp } from "firebase/app";
import { Auth, getAuth } from "firebase/auth";
import { Firestore, getFirestore } from "firebase/firestore";
import {
createContext,
PropsWithChildren,
useContext,
useEffect,
useState,
} from "react";
type FirebaseState = {
firebase: FirebaseApp;
firestore: Firestore;
auth: Auth;
};
const FirebaseContext = createContext<FirebaseState>({} as FirebaseState);
export const FirebaseProvider: React.FC<PropsWithChildren> = ({ children }) => {
const [firebase, setFirebase] = useState<FirebaseApp>();
const [firestore, setFirestore] = useState<Firestore>();
const [auth, setAuth] = useState<Auth>();
useEffect(() => {
setFirebase(initializeApp(FIREBASE_CONFIG));
setFirestore(getFirestore());
setAuth(getAuth());
}, []);
return (
<FirebaseContext.Provider
value={{ firebase, firestore, auth } as FirebaseState}
>
{children}
</FirebaseContext.Provider>
);
};
export const useFirestore = () => {
const { firestore } = useContext(FirebaseContext);
return firestore;
};
export const useFirebaseAuth = () => {
const { auth } = useContext(FirebaseContext);
return auth;
};
export const useFirebase = () => {
const { firebase } = useContext(FirebaseContext);
return firebase;
}; _app.tsx export default App(props: Props) {
const { Component, pageProps } = props;
return (
<FirebaseProvider>
<Component {...pageProps}/>
</FirebaseProvider>
) You can then use whichever hooks you need to do your authentication or firestore querying const Login = () => {
const auth = useFirebaseAuth();
//.... auth logic
return (
<PaddedLayout>
<Typography variant="h3">Login</Typography>
</PaddedLayout>
);
};
export default Login; You can of course use Redux or whatever you want instead of contexts |
Beta Was this translation helpful? Give feedback.
-
You're currently using the Firebase JS SDK during server-side rendering, but it's meant for use on the client side. You'll need to use import React, { useEffect } from 'react'
import { getAuth } from 'firebase/auth'
const ExamplePage = () => {
useEffect(() => {
const auth = getAuth()
// ...
})
// ...
} Here's the example we have in docs. (Taken from this comment thread to make it easier for other people to find the answer.) |
Beta Was this translation helpful? Give feedback.
You're currently using the Firebase JS SDK during server-side rendering, but it's meant for use on the client side. You'll need to use
useEffect
:Here's the example we have in docs.
(Taken from this comment thread to make it easier for other people to find the answer.)