加入自动刷新RefreshToken的逻辑

This commit is contained in:
BBIT-Kai
2025-12-02 13:48:24 +08:00
parent eeada99dbb
commit c53926afd6
12 changed files with 29 additions and 128 deletions
@@ -53,7 +53,7 @@ export const authenticateResponseInterceptor = ({
}: {
client: RequestClient;
doReAuthenticate: () => Promise<void>;
doRefreshToken: () => Promise<string>;
doRefreshToken: () => Promise<null | string>;
enableRefreshToken: boolean;
formatToken: (token: string) => null | string;
}): ResponseInterceptorConfig => {
@@ -88,11 +88,13 @@ export const authenticateResponseInterceptor = ({
try {
const newToken = await doRefreshToken();
if (!newToken) {
throw new Error('刷新 token 失败');
}
// 处理队列中的请求
client.refreshTokenQueue.forEach((callback) => callback(newToken));
// 清空队列
client.refreshTokenQueue = [];
return client.request(error.config.url, { ...error.config });
} catch (refreshError) {
// 如果刷新 token 失败,处理错误(如强制登出或跳转登录页面)