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>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>AI 智能体管理平台</title>
|
<title>驷马智能</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="root"></div>
|
<div id="root"></div>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
const AURA_API_BASE = 'https://api.redhare.cc/aura/v1';
|
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({
|
export const api = axios.create({
|
||||||
baseURL: '/api',
|
baseURL: '/api',
|
||||||
|
|
@ -12,7 +13,7 @@ export const api = axios.create({
|
||||||
api.interceptors.response.use(
|
api.interceptors.response.use(
|
||||||
(r) => r,
|
(r) => r,
|
||||||
(err) => {
|
(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);
|
const next = encodeURIComponent(location.pathname + location.search);
|
||||||
location.href = `/login?next=${next}`;
|
location.href = `/login?next=${next}`;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,18 @@ interface AuthState {
|
||||||
logout: () => Promise<void>;
|
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) => ({
|
export const useAuth = create<AuthState>((set) => ({
|
||||||
user: null,
|
user: typeof localStorage === 'undefined' ? null : loadSavedUser(),
|
||||||
loading: false,
|
loading: false,
|
||||||
bootstrap: async () => {
|
bootstrap: async () => {
|
||||||
set({ loading: false });
|
set({ loading: false });
|
||||||
|
|
@ -20,14 +30,18 @@ export const useAuth = create<AuthState>((set) => ({
|
||||||
login: async (email, password) => {
|
login: async (email, password) => {
|
||||||
const ok = await AuthAPI.verify(email, password);
|
const ok = await AuthAPI.verify(email, password);
|
||||||
if (!ok) throw new Error('身份验证失败');
|
if (!ok) throw new Error('身份验证失败');
|
||||||
set({
|
const u: AuthUser = {
|
||||||
user: {
|
id: 'mock',
|
||||||
id: 'mock',
|
email,
|
||||||
email,
|
name: email.split('@')[0] || 'User',
|
||||||
name: email.split('@')[0] || 'User',
|
role: 'user'
|
||||||
role: 'user'
|
};
|
||||||
}
|
try {
|
||||||
});
|
localStorage.setItem('mock-auth', '1');
|
||||||
|
localStorage.setItem('mock-user', JSON.stringify(u));
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
|
set({ user: u });
|
||||||
},
|
},
|
||||||
register: async (p) => {
|
register: async (p) => {
|
||||||
const u = await AuthAPI.register(p);
|
const u = await AuthAPI.register(p);
|
||||||
|
|
@ -38,6 +52,11 @@ export const useAuth = create<AuthState>((set) => ({
|
||||||
await AuthAPI.logout();
|
await AuthAPI.logout();
|
||||||
} catch {
|
} catch {
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
localStorage.removeItem('mock-auth');
|
||||||
|
localStorage.removeItem('mock-user');
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
set({ user: null });
|
set({ user: null });
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue