feat(auth): 实现全自动无感续杯 (401/即将过期自动重试换票) 与企业多身份唯一标识分层

This commit is contained in:
gitea committed 2026-08-27 11:04:41 +08:00
1 parent 189415cdac
commit b7a3bf2c03
7 files changed
+122 -17

No files matched your search

+25
View File
@@ -37,6 +37,31 @@ export function createAuthClient(options: CreateAuthClientOptions): KyInstance {
}
},
],
afterResponse: [
async ({ request, response }) => {
if (!response) return response;
const errorType = response.headers.get("error-type");
const isAuthError =
response.status === 401 ||
errorType === "UnauthenticatedException" ||
errorType === "UnauthorizedException";
if (isAuthError && options.onUnauthorized) {
// 触发自动续杯刷新 Token
await options.onUnauthorized();
// 用新 Token 重新发送一次请求
const freshHeaders = await options.getHeaders();
for (const [key, value] of Object.entries(freshHeaders)) {
if (value !== undefined) {
request.headers.set(key, value);
}
}
return ky(request);
}
return response;
},
],
beforeRetry: [
async ({ request }) => {
if (options.onUnauthorized) {