feat: 添加管理员实验板管理界面
This commit is contained in:
@@ -1,93 +1,122 @@
|
||||
import { DataClient } from '@/APIClient'
|
||||
import { DataClient } from "@/APIClient";
|
||||
|
||||
export class AuthManager {
|
||||
// 存储token到localStorage
|
||||
public static setToken(token: string): void {
|
||||
localStorage.setItem('authToken', token)
|
||||
localStorage.setItem("authToken", token);
|
||||
}
|
||||
|
||||
|
||||
// 从localStorage获取token
|
||||
public static getToken(): string | null {
|
||||
return localStorage.getItem('authToken')
|
||||
return localStorage.getItem("authToken");
|
||||
}
|
||||
|
||||
|
||||
// 清除token
|
||||
public static clearToken(): void {
|
||||
localStorage.removeItem('authToken')
|
||||
localStorage.removeItem("authToken");
|
||||
}
|
||||
|
||||
|
||||
// 检查是否已认证
|
||||
public static async isAuthenticated(): Promise<boolean> {
|
||||
return await AuthManager.verifyToken()
|
||||
return await AuthManager.verifyToken();
|
||||
}
|
||||
|
||||
|
||||
// 为HTTP请求添加Authorization header
|
||||
public static addAuthHeader(client: any): void {
|
||||
const token = AuthManager.getToken()
|
||||
const token = AuthManager.getToken();
|
||||
if (token && client.http) {
|
||||
const originalFetch = client.http.fetch
|
||||
const originalFetch = client.http.fetch;
|
||||
client.http.fetch = (url: RequestInfo, init?: RequestInit) => {
|
||||
if (!init) init = {}
|
||||
if (!init.headers) init.headers = {}
|
||||
|
||||
if (!init) init = {};
|
||||
if (!init.headers) init.headers = {};
|
||||
|
||||
// 添加Authorization header
|
||||
if (typeof init.headers === 'object' && init.headers !== null) {
|
||||
(init.headers as any)['Authorization'] = `Bearer ${token}`
|
||||
if (typeof init.headers === "object" && init.headers !== null) {
|
||||
(init.headers as any)["Authorization"] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
return originalFetch(url, init)
|
||||
}
|
||||
|
||||
return originalFetch(url, init);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 创建已配置认证的API客户端
|
||||
public static createAuthenticatedClient(): DataClient {
|
||||
const client = new DataClient()
|
||||
AuthManager.addAuthHeader(client)
|
||||
return client
|
||||
const client = new DataClient();
|
||||
AuthManager.addAuthHeader(client);
|
||||
return client;
|
||||
}
|
||||
|
||||
|
||||
// 登录函数
|
||||
public static async login(username: string, password: string): Promise<boolean> {
|
||||
public static async login(
|
||||
username: string,
|
||||
password: string,
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const client = new DataClient()
|
||||
const token = await client.login(username, password)
|
||||
|
||||
const client = new DataClient();
|
||||
const token = await client.login(username, password);
|
||||
|
||||
if (token) {
|
||||
AuthManager.setToken(token)
|
||||
|
||||
AuthManager.setToken(token);
|
||||
|
||||
// 验证token
|
||||
const authClient = AuthManager.createAuthenticatedClient()
|
||||
await authClient.testAuth()
|
||||
|
||||
return true
|
||||
const authClient = AuthManager.createAuthenticatedClient();
|
||||
await authClient.testAuth();
|
||||
|
||||
return true;
|
||||
}
|
||||
return false
|
||||
return false;
|
||||
} catch (error) {
|
||||
AuthManager.clearToken()
|
||||
throw error
|
||||
AuthManager.clearToken();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 登出函数
|
||||
public static logout(): void {
|
||||
AuthManager.clearToken()
|
||||
AuthManager.clearToken();
|
||||
}
|
||||
|
||||
|
||||
// 验证当前token是否有效
|
||||
public static async verifyToken(): Promise<boolean> {
|
||||
try {
|
||||
const token = AuthManager.getToken()
|
||||
const token = AuthManager.getToken();
|
||||
if (!token) {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
|
||||
const client = AuthManager.createAuthenticatedClient()
|
||||
await client.testAuth()
|
||||
return true
|
||||
|
||||
const client = AuthManager.createAuthenticatedClient();
|
||||
await client.testAuth();
|
||||
return true;
|
||||
} catch (error) {
|
||||
AuthManager.clearToken()
|
||||
return false
|
||||
AuthManager.clearToken();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 验证管理员权限
|
||||
public static async verifyAdminAuth(): Promise<boolean> {
|
||||
try {
|
||||
const token = AuthManager.getToken();
|
||||
if (!token) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const client = AuthManager.createAuthenticatedClient();
|
||||
await client.testAdminAuth();
|
||||
return true;
|
||||
} catch (error) {
|
||||
// 如果是401错误,说明token有效但不是管理员,不需要清除token
|
||||
// 如果是其他错误,可能token无效,清除token
|
||||
if (
|
||||
error &&
|
||||
typeof error === "object" &&
|
||||
"status" in error &&
|
||||
error.status !== 401
|
||||
) {
|
||||
AuthManager.clearToken();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user