Fairly experienced with Firebase, encountering a strange and unintuitive bug. I've written a Firebase authentication wrapper using the React Context API, and track the currently logged in user with a listener. Here is my code:
useEffect(() => {
console.log("Registering firebase auth listener");
const unsubscribe = fa.onAuthStateChanged(
fa.getAuth(),
function (user) {
console.log(user);
setCurrentUser(user);
},
(error) => console.error(error),
() => console.warn("Firebase listener unhooked")
);
return unsubscribe;
}, []);
Up until recently, this worked fine, but something has caused it to stop working only on localhost with no error message. The print at the top for checking that the hook runs gets logged, but nothing else.
I don't understand what's going on here. Is there something obvious I've missed?
Solved my problem. I'd accidentally removed the initialization of firebase. To anyone reading this, if your onAuthStateChanged is mysteriously not firing, make sure you run initializeApp first.
If you're importing the intialization from another module, make sure you import the initialization code first.