我将 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
示例用法可以是这样的:
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/