When I log into a firebase app using signInWithEmailAndPassword(email, password)
, the user is logged in. Then, if I reload the page, the user is no longer logged in. If I go to another webpage with the same firebase apiKey and everything, the user still isn't logged in. How would I go about making sure the user stays logged in until firebase.auth().signOut()
is called?
When I log into a firebase app using signInWithEmailAndPassword(email, password)
, the user is logged in. Then, if I reload the page, the user is no longer logged in. If I go to another webpage with the same firebase apiKey and everything, the user still isn't logged in. How would I go about making sure the user stays logged in until firebase.auth().signOut()
is called?
- A quick guess is that you're not monitoring authentication state (see the docs for that). If that's not it, please share the minimal, plete code that allows us to reproduce your problem. – Frank van Puffelen Commented May 23, 2016 at 20:37
-
@FrankvanPuffelen Turns out I was just trying to get
firebase.auth().currentUser
without the state change monitor. I took your advice and it works now. Thanks! – hyperneutrino Commented May 23, 2016 at 20:41 - Also, to whomever close-voted it, how is it off-topic? Please elaborate. – hyperneutrino Commented May 23, 2016 at 20:43
- It works after my small ment. I didn't feel it worth an answer (since I'm pretty sure I've pointed the same out in the past few days), so voted to close as it is unlikely others will find this one. – Frank van Puffelen Commented May 23, 2016 at 20:56
- @FrankvanPuffelen Okay, should I delete it then? – hyperneutrino Commented May 24, 2016 at 12:59
1 Answer
Reset to default 8Simply using firebase.auth().currentUser
usually won't work because when that's loaded, Firebase usually hasn't authenticated and connected a user account yet. Instead, wait for the authorization state to change using firebase.auth().onAuthStateChanged(func)
where func
is called when the auth state is changed (which could mean signing in or signing out or switching accounts).