mirror of
https://github.com/dushixiang/pika.git
synced 2026-05-07 06:16:43 +08:00
fix: 移除会话失效时强制跳转登录页的行为
前端在检测到 401 或本地无 token 时不再自动跳转到 /login, 改为仅清理本地缓存并抛出错误,由调用方自行决定后续行为。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
// 已登录,渲染子组件
|
||||
|
||||
@@ -90,7 +90,6 @@ const AdminLayout = () => {
|
||||
const userInfoStr = localStorage.getItem('userInfo');
|
||||
|
||||
if (!token || !userInfoStr) {
|
||||
navigate('/login');
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user