Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

axios interceptors response undefined

I'm trying to logout my user once they get a 401. I'm using axios to return data from the api

I was looking around and found the same axios.interceptors.response

  axios.interceptors.response.use(
    response => response,
  error => {
    const {status} = error.response;
    if (status === 401 ) {
      store.dispatch('snackBar', snackbarObj)
    } 
   return Promise.reject(error);
  }
)

It appears my error.response is undefined. I'm not sure what is wrong? any ideas?

console.dir of the error

like image 733
ronoc4 Avatar asked Apr 17 '18 19:04

ronoc4


People also ask

How do I intercept a request in Axios?

You can intercept requests or responses before they are handled by then or catch. If you need to remove an interceptor later you can. You can add interceptors to a custom instance of axios. const instance = axios.create(); instance.interceptors.request.use(function () {/*...*/});

Why doesn't Axios set error response when there is an error?

When that happens, it looks like axios doesn't actually set error.response, even though there was a received response. So, at least in my case, the fix was to make sure Access-Control-Allow-Origin: * was included in the response's headers when there was an error.

What is a request/response interceptor?

response interceptor: this is called before the promise is completed and the data is received by the then callback. Think of the interceptor as a tunnel between the request/response and actual promise. So this is part of the third requirement.

What is the use of Axios in react?

Setting token for the instances that required authorisation. Implementing multiple validations to the Header, before the http call is made. Axios is basically an external library, which is used to make promise based HTTP calls (fetch made easy, plus gives us structured responses). Its one of those recommenced libraries, used with ReactJs.


2 Answers

You're not getting a response from the request you're doing with Axios since the browser received a 401 unauthorized response when doing the preflight OPTION request, resulting in a Network Error for the request you're trying to do.

This is related to how CORS works and how your backend handles OPTION requests. To understand how the backend server should handle preflight requests, it's important to understand what is the motivation behind introducing preflight requests.

The backend server should not check for authentication on OPTION requests, it should validate that the request is being made to an endpoint that accepts cross-domain requests and return a success code if it does.

Then, automatically, the browser will proceed with the initially intended request.

That way, the Axios interceptor will receive the 401 error code if the user is no longer authenticated.


Shameless self-promotion, I've published a simple Axios plugin called axios-middleware which helps abstract the use of Axios interceptors in bigger apps. It offers an example of middleware that automatically handles unauthenticated requests by trying to authenticate again before resending the request.

like image 135
Emile Bergeron Avatar answered Oct 04 '22 15:10

Emile Bergeron


For those who still struggling with this, use the following error handling for better control

if (error.response) {
  // Request made and server responded
  console.log(error.response.data);
  console.log(error.response.status);
  console.log(error.response.headers);
} else if (error.request) {
   // The request was made but no response was received
   console.log(error.request);
} else {
   // Something happened in setting up the request that triggered an Error
   console.log('Error', error.message);
}
return Promise.reject(error);
like image 32
4givN Avatar answered Oct 04 '22 14:10

4givN