How to persist firebase auth login across sessions

197 views
Skip to first unread message

Vijay Iyer

unread,
Apr 9, 2023, 11:40:23 PM4/9/23
to Firebase Google Group
Firebase Authentication - When I login with signInWithUserEmailAndPassword, and then listen on the method onAuthStateChanged to get the 'user' object. In a React app, I set this to a useState variable. How do I make sure that a user does not have to login again when they visit the page again?
 Below is a sample code snippet, this runs each time user starts the app / refreshes the page. However, if the app is deployed on a server, then how would this user remain logged in so that when they close the browser / restart the computer, the below code snipped passes through to get to the menu route?
useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, (user) => {
setCurrentUser(user);
if (user !== null) {
navigate("/menu");
}
});
return unsubscribe;
}, []);
Reply all
Reply to author
Forward
0 new messages