I base on Vue, Vuex
- Store
export default {
state: {
currentUser: null,
},
},
mutations: {
updateCurrentUser(state, user) {
state.currentUser = user
},
},
actions: {
// Login
login({ commit }, formData) {
return new Promise((resolve, reject) => {
Meteor.loginWithPassword(
formData.username,
formData.password,
error => {
if (error) {
reject(error)
} else {
commit('updateCurrentUser', Meteor.user())
resolve('success')
}
}
)
})
},
// Call this on `created of AppLayout`
loadCurrentUser({ commit }) {
Tracker.autorun(() => {
if (Meteor.user()) {
console.log('Action: loadCurrentUser')
commit('updateCurrentUser', Meteor.user())
}
})
},
},
}
- Router
// import store ........
router.beforeEach((to, from, next) => {
NProgress.start()
// Check user
if (store.currentUser) {
next()
} else {
next({ path: '/' })
}
But problem when I refresh page, get currentUser = null