fix(web): clear stale auth before workspace load
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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 />;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user