fix: 移除会话失效时强制跳转登录页的行为

前端在检测到 401 或本地无 token 时不再自动跳转到 /login,
改为仅清理本地缓存并抛出错误,由调用方自行决定后续行为。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
yumusb
2026-04-15 20:38:11 +08:00
parent ab22f9bd57
commit d56251a4e7
3 changed files with 2 additions and 5 deletions

View File

@@ -1,4 +1,3 @@
import { Navigate } from 'react-router-dom';
import type { ReactNode } from 'react';
interface PrivateRouteProps {
@@ -13,9 +12,9 @@ const PrivateRoute = ({ children }: PrivateRouteProps) => {
const token = localStorage.getItem('token');
const userInfo = localStorage.getItem('userInfo');
// 如果没有登录信息,重定向到登录页
// 如果没有登录信息,返回空占位
if (!token || !userInfo) {
return <Navigate to="/login" replace />;
return null;
}
// 已登录,渲染子组件

View File

@@ -90,7 +90,6 @@ const AdminLayout = () => {
const userInfoStr = localStorage.getItem('userInfo');
if (!token || !userInfoStr) {
navigate('/login');
return;
}

View File

@@ -81,7 +81,6 @@ const sendRequest = async <T>(url: string, config: RequestConfig = {}): Promise<
if (response.status === 401) {
localStorage.removeItem('token');
localStorage.removeItem('userInfo');
window.location.href = '/login';
throw new HttpError('未认证或认证已过期', {
status: response.status,
statusText: response.statusText,