Skip to content

Instantly share code, notes, and snippets.

@nazrdogan
Created July 7, 2017 13:08
Show Gist options
  • Save nazrdogan/5746325ab82e1e1ea32dcef23785e2f8 to your computer and use it in GitHub Desktop.
Save nazrdogan/5746325ab82e1e1ea32dcef23785e2f8 to your computer and use it in GitHub Desktop.
Axios- 401 Re Login
// Add a request interceptor
axios.interceptors.request.use(function (config) {
// Do something before request is sent
return config;
}, function (error) {
// Do something with request error
return Promise.reject(error);
});
// Add a response interceptor
axios.interceptors.response.use(function (response) {
// Do something with response data
return response;
}, function (error) {
let originalRequest = error.config
if (error.response !== undefined && error.response.status === 401 && !originalRequest._retry) {
return Login(loggedUser).then((loginresponse) => {
const params = loginresponse;
originalRequest.headers['Authorization'] = 'Bearer ' + params.access_token;
return axios(originalRequest);
});
}
// Do something with response error
return Promise.reject(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment