fix(web): clear stale auth before workspace load

This commit is contained in:
Xiaoxia AI
2026-06-22 20:41:48 +08:00
parent fb633a8ed4
commit 14f72dbb50
2 changed files with 5 additions and 3 deletions
+2 -2
View File
@@ -3,6 +3,7 @@
* 封装 Axios 实例,配置拦截器和 Token 管理
*/
import axios, { AxiosError, InternalAxiosRequestConfig } from 'axios';
import { useAuthStore } from '@/store/authStore';
// 创建 Axios 实例
const apiClient = axios.create({
@@ -32,8 +33,7 @@ apiClient.interceptors.response.use(
(response) => response,
async (error: AxiosError) => {
if (error.response?.status === 401) {
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
useAuthStore.getState().clearAuth();
}
return Promise.reject(error);
+3 -1
View File
@@ -2,6 +2,7 @@
* 更新路由,添加 Admin 和 Billing 页面
*/
import { createBrowserRouter, Navigate } from 'react-router-dom';
import React from 'react';
import MainLayout from '@/components/layout/MainLayout';
import Login from '@/pages/auth/Login';
import Register from '@/pages/auth/Register';
@@ -12,8 +13,9 @@ import { useAuthStore } from '@/store/authStore';
// 受保护的路由组件
const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
const hasAccessToken = Boolean(localStorage.getItem('access_token'));
if (!isAuthenticated) {
if (!isAuthenticated || !hasAccessToken) {
return <Navigate to="/login" replace />;
}