I'm trying to authenticate using firebase in react. This is the component that tracks my authentification state.
import { useState} from "react";
function useAuth(fbAuth) {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const createEmailUser = (email, password) => fbAuth.createUserWithEmailAndPassword(email, password);
const signInEmailUser = (email, password) => fbAuth.signInWithEmailAndPassword(email, password);
const signOut = fbAuth.signOut();
fbAuth.onAuthStateChanged(async user=> {
if (user) {
await setIsAuthenticated(true);
console.log(isAuthenticated, "should be true")
return
} else {
await setIsAuthenticated(false);
console.log(isAuthenticated, "should be false")
return
}
});
return {isAuthenticated, createEmailUser, signInEmailUser, signOut};
}
export default useAuth
The console logs when I click log in are
2useAuth.js:13 false "should be true"
2useAuth.js:17 false "should be false"
2useAuth.js:17 true "should be false"
4useAuth.js:17 false "should be false"