I got the following code :
loggedInAxios.interceptors.request.use(
async (config) => {
if (isTokenExpired('access_token')) {
const response = await getRefreshToken();
await refreshAccessToken(response);
}
const accessToken = localStorage.getItem('access_token');
config.headers.Authorization = `Bearer ${accessToken}`;
return config;
},
(error) => error
);
But typescript is complaining that config.headers.Authorization object is possibly undefined.
I found a way by adding the following:
if (!config) {
config = {};
}
if (!config.headers) {
config.headers = {};
}
But I do not think that this is the best way to do it...