How to access store context within store module but outside the action handler?

Viewed 43

I'm trying to access the commit method in a Nuxt store module but outside the default action handler. How can I achieve this?

store/user.js

export const state = () => ({
  user: null,
  authIsReady: false
})

export const mutations = {
  SET_USER(state, user) {
    state.user = user
  },
  SET_AUTH_READY(state, payload) {
    state.authIsReady = payload
  }
}

const unsub = onAuthStateChanged(auth, (user) => {
  // How do I use 'commit' here? This is undefined
  commit('SET_AUTH_READY', true)
  commit('SET_USER', user)
  unsub()
})

Please note that onAuthStateChanged is a function from Firebase.

1 Answers

You need to pass commit as a prop, try this:

const unsub = onAuthStateChanged({auth, commit}, (user) => {
  // How do I use 'commit' here? This is undefined
  commit('SET_AUTH_READY', true)
  commit('SET_USER', user)
  unsub()
})
Related