feat(auth): 添加401未授权处理,清除token并重定向到登录页面
This commit is contained in:
@@ -36,6 +36,16 @@ export async function request(endpoint, options = {}) {
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
// Handle 401 Unauthorized
|
||||
if (res.status === 401) {
|
||||
localStorage.removeItem('token');
|
||||
localStorage.removeItem('user');
|
||||
// Redirect to login if not already there
|
||||
if (!window.location.pathname.startsWith('/auth/login')) {
|
||||
window.location.href = '/auth/login';
|
||||
}
|
||||
}
|
||||
|
||||
// Handle errorx response { code, message, error_id }
|
||||
const errorMsg = data.message || `Request failed with status ${res.status}`;
|
||||
throw new Error(errorMsg);
|
||||
|
||||
Reference in New Issue
Block a user