vue-resource interceptor for auth headers

后端 未结 2 1013
独厮守ぢ
独厮守ぢ 2020-12-31 09:26

I am trying to set up a Vuejs fronted application (vue-cli webpack template) to sit on top of my Laravel API.

I am able to get a successful response from the API wit

相关标签:
2条回答
  • 2020-12-31 09:41

    It turns out my problem was the syntax for which I was setting the headers in the interceptor.

    It should be like this:

    Vue.use(VueResource)
    
    Vue.http.interceptors.push((request, next) => {
      request.headers.set('Authorization', 'Bearer eyJ0e.....etc')
      request.headers.set('Accept', 'application/json')
      next()
    })
    

    While I was doing this:

    Vue.use(VueResource)
    
    Vue.http.interceptors.push((request, next) => {
      request.headers['Authorization'] = 'Bearer eyJ0e.....etc'
      request.headers['Accept'] = 'application/json'
      next()
    })
    
    0 讨论(0)
  • 2020-12-31 09:49

    Add this option:

    Vue.http.options.credentials = true;
    

    And use the interceptors for global way:

    Vue.http.interceptors.push(function(request, next) {
    
    request.headers['Authorization'] = 'Basic abc' //Base64
    request.headers['Accept'] = 'application/json'
    next()
    

    });

    0 讨论(0)
提交回复
热议问题