I have created a Next.js application and am using Firebase authentication. I have used the useContext hook for managing user state across my application.
The code for the AuthContext is as follows:
auth.js
import { createContext, useState, useEffect, useContext } from "react";
import { getAuth, onIdTokenChanged } from "firebase/auth";
const AuthContext = createContext({});
export const AuthProvider = ({children}) => {
const auth = getAuth();
const [user, setUser] = useState(null);
useEffect(() => {
return(onIdTokenChanged(auth, (user) => {
if(user) {
setUser(user);
} else {
setUser(null);
}
}))
},[]);
return(<AuthContext.Provider value={{user}}>{children}</AuthContext.Provider>);
}
export const useAuth = () => useContext(AuthContext);
However, I’m getting the following error in the auth.js file:
- I am not able to understand how to fix it.
- Also, I want to know if using useContext() hook is better for route protection as opposed to storing user session cookies in the browser and verifying it from there.
Edit:
I have configured Firebase in firebaseConfig.js. The code for it is as follows:
firebaseConfig.js
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
};
// Initialize Firebase
export const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
8
Answers
I was just getting the same error, I managed to fix this by doing:
And adding these lines like that:
Initialize app like Marcos Oliveira said, with error handling:
you just need to export firebase conifgurations as app or any other type and re import it inside the page you are working on.
for me it was like this
For anyone still dealing with this issue, my solution was to re-order my imports in the App.js.
For some reason, the components I imported needed to be in a specific order. Otherwise, the same error is thrown.
I did solve the problem by:
first I created a firebaseConfig.js file and put my configs and exported auth
then in authContext.js, I imported auth from firebaseConfig.js
So, for me, that error came about when I had all my firebase configurations on separate page/file (Firebase.js) including the following:
So, the initial setup for Firebase was okay.
However, I also had a separate page/file (App.js) where I called
createUserWithEmailAndPassword(auth, email, password)
as well as other built-in Firebase auth methods…But, I forgot to import app from Firebase.js and that error popped up after I called
createUserWithEmailAndPassword
.Once I did imported app, the error went away.
the problem is with the app object which is not getting initialized before the context provider render’s
You should import the ‘app’ from firebaseConfig.js or whatever.js file your firebase configuration is in, into your Context file.
and in useEffect check for the ‘app’ if it exists then run the firebase-specific functions afterward and also add the ‘app’ to the dependency array.
Just provide app inside the getAuth() function
CreateUser File
FireBaseConfig file