vue.js - How can I use vue-router's Navigation Guards with Vuex and VueJs 2? -
following store:
export const store = new vuex.store({ state: { currentuser: null }, getters, mutations, actions });
this loginuser() action:
loginuser(context, payload) { return new promise(function(resolve, reject) { auth.emailsignin({ email: payload.email, password: payload.password }) .then(function(resp){ context.commit('setcurrentuser', resp.data); resolve(); }); }); }
this changes currentuser state through mutation. after user logged in trying redirect him home page following method in vue component:
methods: { loginuser(){ var = this; this.$store.dispatch({ type: 'loginuser', email: this.email, password: this.password }).then(function(){ that.$router.push('/'); }); } }
so guarding home route following beforeeach method:
router.beforeeach((to, from, next) => { if (to.matched.some(record => record.meta.requireslogin)) { auth.validatetoken() .then(function(){ next() }) .fail(function(){ next({ path: '/login' }) }); } else { next() } })
but happens after login goes home route redirects /login.
how can resolved?
Comments
Post a Comment