async function processQueue(newAccessToken?: string, error?: any) {
while (requestQueue.length) {
const queued = requestQueue.shift();
if (!queued) continue;
if (error) {
queued.reject(error);
} else {
try {
// Explicitly set the new token on the queued request config
// instead of relying on the interceptor (which reads stale cookies)
if (newAccessToken) {
queued.config.headers = {
...queued.config.headers,
Authorization: \`Bearer ${newAccessToken}\`,
};
}
const response = await http(queued.config);
queued.resolve(response);
} catch (err) {
queued.reject(err);
}
}
}
}
try {
const newAccessToken = await handleTokenRefresh();
// process all queued requests with the new token
await processQueue(newAccessToken);
// retry the original request with the new token explicitly
originalRequest!.headers.Authorization = \`Bearer ${newAccessToken}\`;
const retryResponse = await http(originalRequest!);
response.data = retryResponse.data.Output;
response.messages = \["Your request was processed successfully."\];
} finally {
isRefreshing = false;
}