fix(auth): keep mock login and update title
- index.html 标题更新为 驷马智能 - mock 登录持久化到 localStorage,刷新不丢失 - mock 模式下 401 不再自动重定向回 /login,避免无法进入内部页面main
parent
889a58b4e8
commit
e474b4578d
|
|
@ -3,7 +3,7 @@
|
|||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>AI 智能体管理平台</title>
|
||||
<title>驷马智能</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
|
|
|
|||
|
|
@ -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}`;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,8 +11,18 @@ interface AuthState {
|
|||
logout: () => Promise<void>;
|
||||
}
|
||||
|
||||
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<AuthState>((set) => ({
|
||||
user: null,
|
||||
user: typeof localStorage === 'undefined' ? null : loadSavedUser(),
|
||||
loading: false,
|
||||
bootstrap: async () => {
|
||||
set({ loading: false });
|
||||
|
|
@ -20,14 +30,18 @@ export const useAuth = create<AuthState>((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<AuthState>((set) => ({
|
|||
await AuthAPI.logout();
|
||||
} catch {
|
||||
}
|
||||
try {
|
||||
localStorage.removeItem('mock-auth');
|
||||
localStorage.removeItem('mock-user');
|
||||
} catch {
|
||||
}
|
||||
set({ user: null });
|
||||
}
|
||||
}));
|
||||
|
|
|
|||
Loading…
Reference in New Issue