vue-authenticate icon indicating copy to clipboard operation
vue-authenticate copied to clipboard

when reloading site interceptors do not work anymore

Open Snapu opened this issue 6 years ago • 1 comments

I use the following code it works until I reload the site. After reloading it doesn't call the interceptor anymore. What the problem? The token is still there in the session store. After relogin it starts working again until reload. Tested in Safari and Chrome. Using custom auth provider.

/**
* This is example for request and response interceptors for axios library
*/

Vue.use(VueAuthenticate, {
  bindRequestInterceptor: function () {
    this.$http.interceptors.request.use((config) => {
      if (this.isAuthenticated()) {
        config.headers['Authorization'] = [
          this.options.tokenType, this.getToken()
        ].join(' ')
      } else {
        delete config.headers['Authorization']
      }
      return config
    })
  },

  bindResponseInterceptor: function () {
    this.$http.interceptors.response.use((response) => {
      this.setToken(response)
      return response
    })
  }
})

Snapu avatar May 22 '18 08:05 Snapu

There is no bindResponseInterceptor option anymore. To solve the problem, just setup both request and response interceptors inside bindRequestInterceptor like this

Vue.use(VueAuthenticate, {
  bindRequestInterceptor: function () {
    this.$http.interceptors.request.use((config) => {
      if (this.isAuthenticated()) {
        config.headers['Authorization'] = [
          this.options.tokenType, this.getToken()
        ].join(' ')
      } else {
        delete config.headers['Authorization']
      }
      return config
    })

    this.$http.interceptors.response.use((response) => {
      this.setToken(response)
      return response
    })
  }
})

mr-july avatar May 17 '19 13:05 mr-july