Reputation: 661
I am trying to solve this but I can't, I have a website built with Laravel and Vuejs: This is my app.js
import 'bootstrap';
import './axios';
import Vue from 'vue';
import VueRouter from 'vue-router';
import store from './store';
import router from './router';
store.dispatch('checkAuth');
const app = new Vue({
el: '#app',
store ,
router
});
this is my router.js:
import VueRouter from 'vue-router';
import Login from './components/Login';
import Register from './components/Register';
import Main from './components/Main';
const checkLogin = (to, from, next) =>{
if (to.matched.some(record => record.meta.requiresAuth)) {
if (!store.getters.isAuthenticated) {
next({name: 'login' })
} else {
next()
}
} else if (to.matched.some(record => record.meta.requiresVisitor)) {
if (store.getters.isAuthenticated) {
next({name: 'home' })
} else {
next()
}
} else {
next()
}
}
const router = new VueRouter({
mode: 'history',
routes: [
{ path: '/',
beforeEnter: checkLogin,
component: Main,
meta: { requiresAuth: true },
children : [
{
path: "",
name: "home",
component: PostsList
},
....
]
},
{
path: '/login',
beforeEnter: checkLogin,
component: Login,
name:'login',
meta: { requiresVisitor: true }
},
{
path: '/register',
beforeEnter: checkLogin,
component: Register,
name: 'register',
meta: { requiresVisitor: true }
}
]
});
and this is my store:
import Vue from 'vue';
const state = {
user: null,
isAuthenticated: false
};
const getters = {
isAuthenticated(state){
return state.isAuthenticated;
},
currentUser(state){
return state.user;
}
};
async login({commit, dispatch}, credentials) {
await axios.get('/sanctum/csrf-cookie');
const {data} = await axios.post("/login", credentials);
commit('setAuth', data);
},
async checkAuth({commit}) {
const {data} = await axios.get('/api/user');
commit('setAuth', data);
}
}
const mutations = {
setAuth(state, payload) {
state.user = payload.user;
state.isAuthenticated = Boolean(payload.user);
}
};
Then the problem arrives when I refresh the page, it calls the action:
store.dispatch('checkAuth');
then it should wait until the action is done because I did this in the action:
const {data} = await axios.get('/api/user');
But no, it doesn't wait because vue-router is executed and as the user state is not set yet , store.getters.isAuthenticated is false then it redirects to /login, then when I check the vue tools in the browser and see that the state was set correctly even the request to api/user returns the user correctly, Because before that I logged in a user, I need that vue-router waits until the vuex state is set. what can I do? Thank you.
Upvotes: 1
Views: 3010
Reputation: 661
I was able to solve this problem, but I don't know if this is the best way to do it:
in my app.js Instead of this:
store.dispatch('checkAuth');
const app = new Vue({
el: '#app',
store ,
router
});
I did this:
const VueInstance = ()=>{
new Vue({
el: '#app',
store ,
router
});
}
store.dispatch('checkAuth').then(()=>{
VueInstance();
}).catch(()=>{
VueInstance();
});
Now it is working because the vuex action checkAuth returns a promise, so I needed to wait until it is completed, but, in case the action returns an error, because in the first load the user is not logged in, I must add a catch, because th Vue should be created whether the user is logged in or not. If someone have a better solution let me know. Thank you.
Upvotes: 1