Static GitHub Issues

[193] nuxtServerInit just modify state in the same file

prev: Server won't run/compile/display
next: Build client-only bundle

I was trying to create a module "auth" separate from the main module "index.js", but when I try to modify the state of the module "auth" through the action nuxtServerInit simply does not work.

My index.js file

export default {
  actions: {
    nuxtServerInit({commit}, {req}) {
      if (req.auth) {
        commit('auth/SET_USER', req.auth)
      }
    }
  },
}

My auth.js file

export default {
  state: {
    user: null
  },
  mutations: {
    SET_USER({user}, payload) {
      user = payload
    }
  }
}

When I try to modify a state within the index.js file it works normally

example

export default {
  state: {
    ping: null
  },
  mutations: {
    SET_PING(state, payload) {
      state.ping = payload
    }
  },
  actions: {
    nuxtServerInit({commit}, {req}) {
      commit('SET_PING', 'PONG')
    }
  },
}
<!--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/c164">#c164</a>)</em></sub></div>