最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase AUTH won't stay logged in - Stack Overflow

programmeradmin1浏览0评论

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?

Share Improve this question edited May 23, 2016 at 20:35 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges asked May 23, 2016 at 20:20 hyperneutrinohyperneutrino 5,4262 gold badges31 silver badges52 bronze badges 8
  • 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
 |  Show 3 more ments

1 Answer 1

Reset to default 8

Simply 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).

发布评论

评论列表(0)

  1. 暂无评论