Static GitHub Issues

[655] Router middleware doesnt commit to store on SSR

prev: error for middleware
next: GraphQL Server inside Nuxt.js

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?

<!--cmty--><!--cmty_prevent_hook--><div align="right"><sub><em>This question is available on <a href="https://nuxtjs.cmty.io">Nuxt.js</a> community (<a href="https://nuxtjs.cmty.io/nuxt/nuxt.js/issues/c568">#c568</a>)</em></sub></div>