This is the store.js
which handle vuex store and login and logout functions,
actions:{
login({ commit }, creds){
commit(LOGIN);
return new Promise((resolve) => {
setTimeout(() =>{
axios.post('/oauth/token', creds).then((response) =>{
Vue.auth.setToken(response.data.access_token, response.data.expires_in + Date.now());
commit(LOGIN_SUCCESS);
});
resolve();
}, 3000);
});
},
logout({commit}){
return new Promise((resolve) => {
setTimeout(() =>{
Vue.auth.destroyToken();
commit(LOGOUT);
resolve();
}, 3000);
});
console.log('Successfully logged out');
}
}
and login component is this,
login(){
try{
this.$store.dispatch('login', {
username: this.user.email,
password: this.user.password,
client_id: 2,
client_secret: 'wcw9GkgKMHXUnyZavawJWXgE3GhSubOADO6tKw99',
grant_type: 'password'
})
} catch(error) {
console.log('error');
}
finally{
alert('Router Push')
this.$router.push('/dashboard');
console.log('success')
}
}
the problem is no matter where i place router.push
its not working, but alert
and console.log
is working fine but page is not pushing to a specified path, after page fresh it works. But not soon after the alert pops up. What's wrong here ? Thanks in advance.
here is navigation guard
router.beforeEach((to, from, next) =>{
if(to.matched.some(record => record.meta.forVisitor)){
if(Vue.auth.isAuthenticated())
{
next({
path: '/dashboard'
});
} else {
next();
}
} else if(to.matched.some(record => record.meta.isauth)){
if( !Vue.auth.isAuthenticated())
{
next({
path: '/login'
});
} else {
next();
}
} else {
next()
}
});