From e474b4578d8acf20a7eb66bbb7501e749ca67bf1 Mon Sep 17 00:00:00 2001 From: sp mac bookpro 2605 Date: Sun, 24 May 2026 17:37:45 +0800 Subject: [PATCH] fix(auth): keep mock login and update title MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - index.html 标题更新为 驷马智能 - mock 登录持久化到 localStorage,刷新不丢失 - mock 模式下 401 不再自动重定向回 /login,避免无法进入内部页面 --- index.html | 2 +- src/api.ts | 3 ++- src/store/auth.ts | 37 ++++++++++++++++++++++++++++--------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/index.html b/index.html index 86a3c58..b560476 100644 --- a/index.html +++ b/index.html @@ -3,7 +3,7 @@ - AI 智能体管理平台 + 驷马智能
diff --git a/src/api.ts b/src/api.ts index fdc6602..72aba84 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,6 +1,7 @@ import axios from 'axios'; const AURA_API_BASE = 'https://api.redhare.cc/aura/v1'; +const isMockAuth = () => typeof localStorage !== 'undefined' && localStorage.getItem('mock-auth') === '1'; export const api = axios.create({ baseURL: '/api', @@ -12,7 +13,7 @@ export const api = axios.create({ api.interceptors.response.use( (r) => r, (err) => { - if (err?.response?.status === 401 && !location.pathname.startsWith('/login')) { + if (err?.response?.status === 401 && !location.pathname.startsWith('/login') && !isMockAuth()) { const next = encodeURIComponent(location.pathname + location.search); location.href = `/login?next=${next}`; } diff --git a/src/store/auth.ts b/src/store/auth.ts index e89e53c..92c71ea 100644 --- a/src/store/auth.ts +++ b/src/store/auth.ts @@ -11,8 +11,18 @@ interface AuthState { logout: () => Promise; } +const loadSavedUser = (): AuthUser | null => { + try { + const raw = localStorage.getItem('mock-user'); + if (!raw) return null; + return JSON.parse(raw) as AuthUser; + } catch { + return null; + } +}; + export const useAuth = create((set) => ({ - user: null, + user: typeof localStorage === 'undefined' ? null : loadSavedUser(), loading: false, bootstrap: async () => { set({ loading: false }); @@ -20,14 +30,18 @@ export const useAuth = create((set) => ({ login: async (email, password) => { const ok = await AuthAPI.verify(email, password); if (!ok) throw new Error('身份验证失败'); - set({ - user: { - id: 'mock', - email, - name: email.split('@')[0] || 'User', - role: 'user' - } - }); + const u: AuthUser = { + id: 'mock', + email, + name: email.split('@')[0] || 'User', + role: 'user' + }; + try { + localStorage.setItem('mock-auth', '1'); + localStorage.setItem('mock-user', JSON.stringify(u)); + } catch { + } + set({ user: u }); }, register: async (p) => { const u = await AuthAPI.register(p); @@ -38,6 +52,11 @@ export const useAuth = create((set) => ({ await AuthAPI.logout(); } catch { } + try { + localStorage.removeItem('mock-auth'); + localStorage.removeItem('mock-user'); + } catch { + } set({ user: null }); } }));