admin管理员组文章数量:1401417
Alright, So I'd like to add a response interceptor to my global axios config, that retries a request once if it gets a 401 error, after refreshing the token.
This is my current global axios config:
import axios from "axios";
export default axios.create({
baseURL: process.env.REACT_APP_API,
headers: {
"content-type": "application/json"
},
responseType: "json"
});
Now I've did research my issue has been addressed here, however, I'm not aware how I can attach or use the interceptor function in the answer there to my axios config file (after I edit it to my needs ofc) the interceptor function looks like this:
createAxiosResponseInterceptor() {
const interceptor = axios.interceptors.response.use(
response => response,
error => {
// Reject promise if usual error
if (errorResponse.status !== 401) {
return Promise.reject(error);
}
/*
* When response code is 401, try to refresh the token.
* Eject the interceptor so it doesn't loop in case
* token refresh causes the 401 response
*/
axios.interceptors.response.eject(interceptor);
return axios.post('/api/refresh_token', {
'refresh_token': this._getToken('refresh_token')
}).then(response => {
saveToken();
error.response.config.headers['Authorization'] = 'Bearer ' + response.data.access_token;
return axios(error.response.config);
}).catch(error => {
destroyToken();
this.router.push('/login');
return Promise.reject(error);
}).finally(createAxiosResponseInterceptor);
}
);
}
Many thanks!
Alright, So I'd like to add a response interceptor to my global axios config, that retries a request once if it gets a 401 error, after refreshing the token.
This is my current global axios config:
import axios from "axios";
export default axios.create({
baseURL: process.env.REACT_APP_API,
headers: {
"content-type": "application/json"
},
responseType: "json"
});
Now I've did research my issue has been addressed here, however, I'm not aware how I can attach or use the interceptor function in the answer there to my axios config file (after I edit it to my needs ofc) the interceptor function looks like this:
createAxiosResponseInterceptor() {
const interceptor = axios.interceptors.response.use(
response => response,
error => {
// Reject promise if usual error
if (errorResponse.status !== 401) {
return Promise.reject(error);
}
/*
* When response code is 401, try to refresh the token.
* Eject the interceptor so it doesn't loop in case
* token refresh causes the 401 response
*/
axios.interceptors.response.eject(interceptor);
return axios.post('/api/refresh_token', {
'refresh_token': this._getToken('refresh_token')
}).then(response => {
saveToken();
error.response.config.headers['Authorization'] = 'Bearer ' + response.data.access_token;
return axios(error.response.config);
}).catch(error => {
destroyToken();
this.router.push('/login');
return Promise.reject(error);
}).finally(createAxiosResponseInterceptor);
}
);
}
Many thanks!
Share Improve this question asked Nov 7, 2019 at 11:58 Omar HusseinOmar Hussein 1,1472 gold badges15 silver badges33 bronze badges2 Answers
Reset to default 3If I understand the problem correctly, you need to invoke the interceptor creator function on your custom axioms instance. So something like:
import axios from "axios";
const instance = axios.create({
baseURL: process.env.REACT_APP_API,
headers: {
"content-type": "application/json"
},
responseType: "json"
});
function createAxiosResponseInterceptor(axiosInstance) {
const interceptor = axiosInstance.interceptors.response.use(
response => response,
error => {
// Reject promise if usual error
if (errorResponse.status !== 401) {
return Promise.reject(error);
}
/*
* When response code is 401, try to refresh the token.
* Eject the interceptor so it doesn't loop in case
* token refresh causes the 401 response
*/
axiosInstance.interceptors.response.eject(interceptor);
return axiosInstance.post('/api/refresh_token', {
'refresh_token': this._getToken('refresh_token')
}).then(response => {
saveToken();
error.response.config.headers['Authorization'] = 'Bearer ' + response.data.access_token;
return axiosInstance(error.response.config);
}).catch(error => {
destroyToken();
this.router.push('/login');
return Promise.reject(error);
}).finally(createAxiosResponseInterceptor);
}
);
}
createAxiosResponseInterceptor(instance);
export default instance;
And make the createAxiosResponseInterceptor
function take the axios instance as a parameter.
I think you need to call your intercept creator with custom instance of axios. You can do it as
const instance = axios.create({
baseURL: process.env.REACT_APP_API,
headers: {
"content-type": "application/json"
},
responseType: "json"
});
and pass this instance to createAxiosResponseInterceptor as
createAxiosResponseInterceptor(instance);
You would have to change your createAxiosResponseInterceptor to receive this instance as
function createAxiosResponseInterceptor(instance) {
const interceptor = instance.interceptors.response.use(
response => response,
error => {
// Reject promise if usual error
if (errorResponse.status !== 401) {
return Promise.reject(error);
}
/*
* When response code is 401, try to refresh the token.
* Eject the interceptor so it doesn't loop in case
* token refresh causes the 401 response
*/
instance.interceptors.response.eject(interceptor);
return instance.post('/api/refresh_token', {
'refresh_token': this._getToken('refresh_token')
}).then(response => {
saveToken();
error.response.config.headers['Authorization'] = 'Bearer ' + response.data.access_token;
return instance(error.response.config);
}).catch(error => {
destroyToken();
this.router.push('/login');
return Promise.reject(error);
}).finally(createAxiosResponseInterceptor);
}
);
}
and of course export as
export default instance;
Hope it helps
本文标签: javascriptHow to add interceptor function to AxiosStack Overflow
版权声明:本文标题:javascript - How to add interceptor function to Axios? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744276602a2598443.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论