繁体   English   中英

Firebase Auth Web:注销并重新加载页面

[英]Firebase Auth Web: Logout with page reload

我在 VueJS 中使用 Firebase。 到目前为止,注册、登录工作正常,但一旦我用 F5 重新加载页面或直接写入浏览器地址栏加载页面,然后 auth session 被终止,用户不再登录。 只要我不重新加载页面,它就可以正常工作,即使我单击路由链接或被重定向到其他页面,用户 session 仍然存在。 顺便说一句,我在哪里重新加载页面并不重要。

我的登录方式:

firebase.auth().signInWithEmailAndPassword(this.username, this.password).then(
                    (user) => {
                        if (user.emailVerified === false) {
                            firebase.auth().signOut()
                        } else {
                            // redirect to lobby page if user is verified and signed in
                            this.$router.push('/lobby')
                        }
                    },
                    function(err) {
                        console.log(err.message)
                    }
                )

您需要调用firebase.auth().onAuthStateChanged来检测用户是否已登录。

身份验证状态更改时,将在UI线程中调用此方法:

  • 在注册听众之后

  • 用户登录后

  • 当前用户退出时

  • 当前用户更改时

资源

用法示例如下所示:

firebase.auth().onAuthStateChanged(user => {
  if (user) {
    if (user.emailVerified === false) {
      firebase.auth().signOut()
    } else {
      this.$router.push('/lobby')
    }
  } else {
    // will get to here from a logout event
    // this.$router.push('/login')
  }
}

在您的 main.js 文件中使用它:

firebase.auth().onAuthStateChanged(() => {
if (!app) {
    app = createApp(App).use(store).use(router).mount('#app')
}})

Firebase 将在创建 Vue 应用程序之前运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM