feat(request): enhance token refresh handling and public auth request logic
- Introduced a function to identify public authentication requests, preventing unnecessary token refresh attempts for specific endpoints. - Simplified the redirection logic to the login page upon 401 errors, improving code readability and maintainability. - Updated error handling to provide clearer messages for failed authentication attempts. This update improves the request handling process, ensuring a smoother user experience during authentication flows.
This commit is contained in:
@@ -64,7 +64,13 @@ instance.interceptors.request.use(
|
|||||||
// Token刷新标志,防止多个请求同时刷新token
|
// Token刷新标志,防止多个请求同时刷新token
|
||||||
let isRefreshing = false;
|
let isRefreshing = false;
|
||||||
let failedQueue: Array<{ resolve: Function; reject: Function }> = [];
|
let failedQueue: Array<{ resolve: Function; reject: Function }> = [];
|
||||||
let hasRedirectedOn401 = false;
|
|
||||||
|
const PUBLIC_AUTH_PATHS = ['/auth/auto-setup', '/auth/login', '/auth/register', '/auth/oidc/'];
|
||||||
|
|
||||||
|
function isPublicAuthRequest(url?: string): boolean {
|
||||||
|
if (!url) return false;
|
||||||
|
return PUBLIC_AUTH_PATHS.some(p => url.includes(p));
|
||||||
|
}
|
||||||
|
|
||||||
// 处理队列中的请求
|
// 处理队列中的请求
|
||||||
const processQueue = (error: any, token: string | null = null) => {
|
const processQueue = (error: any, token: string | null = null) => {
|
||||||
@@ -79,6 +85,12 @@ const processQueue = (error: any, token: string | null = null) => {
|
|||||||
failedQueue = [];
|
failedQueue = [];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function redirectToLogin() {
|
||||||
|
if (typeof window === 'undefined') return;
|
||||||
|
if (window.location.pathname === '/login') return;
|
||||||
|
window.location.href = '/login';
|
||||||
|
}
|
||||||
|
|
||||||
instance.interceptors.response.use(
|
instance.interceptors.response.use(
|
||||||
(response) => {
|
(response) => {
|
||||||
// 根据业务状态码处理逻辑
|
// 根据业务状态码处理逻辑
|
||||||
@@ -96,10 +108,10 @@ instance.interceptors.response.use(
|
|||||||
return Promise.reject({ message: t('error.networkError') });
|
return Promise.reject({ message: t('error.networkError') });
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果是登录接口的401,直接返回错误以便页面展示toast,不做跳转
|
// 公开接口(auto-setup / login / register / oidc)的 401 不走 refresh 逻辑,直接返回错误
|
||||||
if (error.response.status === 401 && originalRequest?.url?.includes('/auth/login')) {
|
if (error.response.status === 401 && isPublicAuthRequest(originalRequest?.url)) {
|
||||||
const { status, data } = error.response;
|
const { status, data } = error.response;
|
||||||
return Promise.reject({ status, message: (typeof data === 'object' ? data?.message : data) || t('error.invalidCredentials') });
|
return Promise.reject({ status, message: (typeof data === 'object' ? (data?.error?.message || data?.message) : data) || t('error.invalidCredentials') });
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果是401错误且不是刷新token的请求,尝试刷新token
|
// 如果是401错误且不是刷新token的请求,尝试刷新token
|
||||||
@@ -153,11 +165,7 @@ instance.interceptors.response.use(
|
|||||||
|
|
||||||
processQueue(refreshError, null);
|
processQueue(refreshError, null);
|
||||||
|
|
||||||
// 跳转到登录页
|
redirectToLogin();
|
||||||
if (!hasRedirectedOn401 && typeof window !== 'undefined') {
|
|
||||||
hasRedirectedOn401 = true;
|
|
||||||
window.location.href = '/login';
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.reject(refreshError);
|
return Promise.reject(refreshError);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -169,10 +177,7 @@ instance.interceptors.response.use(
|
|||||||
localStorage.removeItem('weknora_user');
|
localStorage.removeItem('weknora_user');
|
||||||
localStorage.removeItem('weknora_tenant');
|
localStorage.removeItem('weknora_tenant');
|
||||||
|
|
||||||
if (!hasRedirectedOn401 && typeof window !== 'undefined') {
|
redirectToLogin();
|
||||||
hasRedirectedOn401 = true;
|
|
||||||
window.location.href = '/login';
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.reject({ message: t('error.pleaseRelogin') });
|
return Promise.reject({ message: t('error.pleaseRelogin') });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user