node.js - Firebase 身份验证网页 : Logout with page reload

标签 node.js firebase vuejs2 firebase-authentication

我将 Firebase 与 VueJS 结合使用。到目前为止,注册、登录工作正常,但一旦我使用 F5 重新加载页面或直接写入浏览器地址栏加载页面,然后身份验证 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 线程中调用此方法:

  • Right after the listener has been registered

  • When a user is signed in

  • When the current user is signed out

  • When the current user changes

Source

示例用法可以是这样的:

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')
  }
}

关于node.js - Firebase 身份验证网页 : Logout with page reload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48696326/

相关文章:

node.js - 两个nodejs应用程序之间的通信

javascript - Firebase 查询示例返回无用的对象

javascript - 使用 v-model 在 v-for 数组中搜索 - Vue.js

firebase - 构建 Builder(dirty) : 引发了以下 RangeError

firebase - 如何在 Flutter 中重置 Firebase 监听器

javascript - Vue.js - 尝试将对象从 data() 传递到 Mounted() 函数

javascript - 使用 Vueify 渲染函数时在模板中绑定(bind) Vue 数据属性值

html - socket.io 更新所有打开的页面

node.js - 为什么我的引用文献不填充文档?

node.js - 关于express.cookieSession()的几个问题