I've created a middleware file called checkAuth.js
:
export default function ({store, redirect, route}) {
if (route.fullPath == '/login' && store.getters.isAuthenticated) {
redirect('/');
} else if (route.fullPath != '/login' && !store.getters.isAuthenticated) {
redirect('/login');
store.commit('SET_REDIRECTED_FROM', route.fullPath);
}
}
and in nuxt.config.js
I've set:
router: {
middleware: 'checkAuth'
}
relevant parts of store:
export const state = {
user: null,
redirectedFrom: null
};
export const mutations = {
SET_USER (state, user) {
state.user = user || null
},
SET_REDIRECTED_FROM (state, from) {
state.redirectedFrom = from || null;
}
};
The issue is that when line store.commit('SET_REDIRECTED_FROM', route.fullPath);
is called in middleware, vuex store doesnt change the relevant state, even though I can see that the commit was actually called. Am I doing something wrong?