E
E
evlgromov2021-01-10 19:19:06
Vue.js
evlgromov, 2021-01-10 19:19:06

How to pass parameter to vue component when using axios interceptor?

In a Vue project, authorization errors are caught in the main.js file, when the token expires, a redirect to the login page is performed, this works without problems. My task now is to catch this error when entering incorrect data and pass the e.response.data.message parameter with an error message from the server to the Login component without router.push. If I use a redirect, I get an error like redirecting from the /login route to /login, avoid this kind of thing. How can e.response.data.message be passed to Login.vue?

main.js

axios.interceptors.response.use(
  res => {
    return res
  },
  error => {
    if(error.response.status === 401) {
      if (router.currentRoute.name !== 'Login') {
        store.dispatch('destroyToken')
        router.push({
          name: 'Login',
          params: {
            message: 'Срок действия токена истек, пожалуйста авторизуйтесь!'
          }
        })
      }
    }
    return Promise.reject(error)
  }
)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
evlgromov, 2021-01-11
@evlgromov

Made by committing errors in the store.

axios.interceptors.response.use(
  res => {
    return res
  },
  error => {
    if(error.response.status === 401) {
      if (router.currentRoute.name !== 'Login') {
        store.dispatch('destroyToken')
        router.push({
          name: 'Login',
          params: {
            message: 'Срок действия токена истек, пожалуйста авторизуйтесь!'
          }
        })
      } else {
        store.commit('createError', error.response.data)
      }
    }
    return Promise.reject(error)
  }
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question