feat(auth): 添加401未授权处理,清除token并重定向到登录页面

This commit is contained in:
2025-12-30 22:14:11 +08:00
parent 7a8c5c4427
commit c8f6cb0b34

View File

@@ -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);