1. Add 登陆功能
2. 调整字体大小 3. 新增部分功能
This commit is contained in:
43
frontend/src/api/auth.ts
Normal file
43
frontend/src/api/auth.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
const AUTH_API_BASE = '/api/v1';
|
||||
|
||||
export interface TokenResponse {
|
||||
access_token: string;
|
||||
token_type: string;
|
||||
expires_in: number;
|
||||
}
|
||||
|
||||
export interface MeResponse {
|
||||
user_id: string;
|
||||
username: string;
|
||||
role: string;
|
||||
}
|
||||
|
||||
export async function loginRequest(
|
||||
username: string,
|
||||
password: string,
|
||||
): Promise<TokenResponse> {
|
||||
const body = new URLSearchParams();
|
||||
body.set('username', username);
|
||||
body.set('password', password);
|
||||
|
||||
const res = await fetch(`${AUTH_API_BASE}/auth/token`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
body: body.toString(),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const payload = await res.json().catch(() => ({})) as { detail?: string };
|
||||
throw new Error(payload.detail ?? `Login failed (${res.status})`);
|
||||
}
|
||||
|
||||
return res.json() as Promise<TokenResponse>;
|
||||
}
|
||||
|
||||
export async function getMeRequest(token: string): Promise<MeResponse> {
|
||||
const res = await fetch(`${AUTH_API_BASE}/auth/me`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
if (!res.ok) throw new Error(`Unauthorised (${res.status})`);
|
||||
return res.json() as Promise<MeResponse>;
|
||||
}
|
||||
Reference in New Issue
Block a user