I create my instance axios with axios.create in my Api.js
import axios from 'axios';
const token = "M8uqVtkmHWAV3K2PaSZYLKkHWqeCWd22cxGNPXYnpqeT3US"
export default () => {
let instance = axios.create({
baseURL: process.env.REACT_APP_API_ENDPOINT,
headers: {
Authorization: `Bearer ${token}`
}
})
instance.interceptors.request.use(request => {
return request;
})
instance.interceptors.response.use( response => {
return response.data;
})
return instance
}
How can I change the bearer token ?
I'm thinking of deleting the instance and recreating a new one with another token bearer.
Is there a better way to do it?
We can give a header parameter in the interceptors. Thanks to Puneet Bhandari.
axios.interceptors.request.use(function(config) {
const token = cookie.get(__TOKEN_KEY__);
if ( token != null ) {
config.headers.Authorization = `Bearer ${token}`;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.