So I was playing arround with onAuthStateChanged listeners, reading the firebase docs, the current user also can be gotten using auth.currentUser (in useEffect), when the Context provider mounts. Althought there is a user logged in, auth.currentUser returned null. Is there a problem with the code?
const Component= () => {
const { currentUser, setCurrentUser } = useContext(UserContext);
const getCurrentUser = () => {
const auth = getAuth();
const user = auth.currentUser;
return user;
}
useEffect(() => {
const user = getCurrentUser();
if(user){
setCurrentUser(prevUser => user);
}
else setCurrentUser(prevUser => null);
}, [currentUser])
return (
<UserContext.Provider value={{ currentUser, setCurrentUser }}>
{children}
</UserContext.Provider>
)
}
onAuthStateChanged will return the current active user, but auth.currentUser returned null .