A
A
Alline2020-10-28 17:51:29
JavaScript
Alline, 2020-10-28 17:51:29

Vuex, authorization. Why do getters return undefined?

I'm trying to make authorization on Vue.js.
There is a registration method. He works fine.

registration ({commit}, data) {
    return new Promise((resolve, reject) => {
      axios
        .post(localStorage.getItem('url') + 'user/add', data)
        .then(response => {
          const token = response.data.result.token
          console.log(token)
          localStorage.setItem('token', token)
          axios.defaults.headers.common['Authorization'] = token
          commit('AUTH_SUCCESS', token)
          resolve(response)
        })
        .catch(err => {
          commit('auth_error', err)
          localStorage.removeItem('token')
          reject(err)
        })
    })
  },

The token is stored in localStorage, the AUTH_SUCCESS mutation is also performed, everything seems to be ok.
AUTH_SUCCESS (state, token) {
    state.status = 'success'
    state.token = token
  },


But when I later try to check with getters, it returns undefined.
Getters:
const getters = {
  isLoggedIn: state => !!state.token,
  authStatus: state => state.status
}

In the router, beforeEach console.log(store.getters.isLoggedIn)is already undefined.
console.log(store.getters.authStatus)- also
store is imported into the router. Tell me pliz where to look, what's wrong?

I'm not a front, so sorry if the question is stupid or crookedly asked.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question