[Vuejs]-How logout user when one of APIs responds Unauthorized 401 (Nuxt-Auth)

5👍

Since you’re using Axios, it’ll be easy to use Interceptors and catch the error according to your requirement and then call the logout action

Something like below with a plugin created in src/plugins/axios.js will work

export default function ({ $axios }) {
  $axios.onError((error) => {
    if (error.response.status === 401) {
      CALL_LOGOUT_ACTION_HERE
    }
  })
}

1👍

pass context instead of $axios:

export default function (context) {
    context.$axios.onError((error) => {
        if (error.response.status === 401) {
            context.$auth.logout()
        }
    })
}

Leave a comment