Created
July 7, 2017 13:08
-
-
Save nazrdogan/5746325ab82e1e1ea32dcef23785e2f8 to your computer and use it in GitHub Desktop.
Axios- 401 Re Login
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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