【发布时间】:2018-01-03 14:33:14
【问题描述】:
我想在我的 VueJS-Application 中触发 Firebase 的 onAuthStateChanged-method。
目前我无法以某个用户的身份保持登录状态,onAuthStateChanged-方法似乎没有运行。
我的动作是由...开始的
beforeCreate() {
this.$store.dispatch('update_session')
}
...在我的主应用程序组件中。
突变看起来像:
update_session(state) {
auth.onAuthStateChanged().then((user) => {
if (user) {
// USE USER KEY
state.user.current.key = user.uid
// USE USER ROLE
users.child(state.user.current.key).on('value', (snapshot) => {
state.user.current.role = snapshot.val().role
})
}
})
}
编辑:
当然我正在使用一个动作:
update_session({ commit }) {
commit('update_session')
}
【问题讨论】:
-
onAuthStateChanged不返回承诺。所以我认为那行应该是auth.onAuthStateChanged((user) => {,而不是auth.onAuthStateChanged().then((user) => { -
onAuthStateChanged 会在每次身份验证状态更改时被调用,例如,当您重新加载页面时,它将使用本地存储中的 firebase 数据调用。您将需要在我的 main.js 文件中全局处理此问题。
-
谢谢你,你已经做到了。谢谢。 :D
-
@phoet 为什么不把这个作为答案?