Files
xinfra/frontend/src/api/auth.ts
T

57 lines
1.3 KiB
TypeScript
Raw Normal View History

import type { ApiResponse } from '@/types/api'
import { getToken } from '@/utils/auth'
2026-07-14 15:39:32 +08:00
export interface LoginRequest {
username: string
password: string
}
export interface UserInfo {
id: number
username: string
display_name: string
email: string
business_line: string
}
export interface LoginResponse {
token: string
expires_in: number
user: UserInfo
}
export const authApi = {
login(_data: LoginRequest): Promise<ApiResponse<LoginResponse>> {
return Promise.reject(new Error('password login is disabled'))
2026-07-14 15:39:32 +08:00
},
logout(): Promise<ApiResponse<null>> {
return Promise.resolve({ code: 0, message: 'success', data: null })
2026-07-14 15:39:32 +08:00
},
async getUserInfo(): Promise<ApiResponse<UserInfo>> {
const token = getToken()
const response = await fetch('/auth/api/v1/users/me', {
headers: {
Accept: 'application/json',
...(token ? { Authorization: `Bearer ${token}` } : {}),
},
2026-07-14 15:39:32 +08:00
})
const data = await response.json().catch(() => ({}))
if (!response.ok) {
throw new Error(data.error || `HTTP ${response.status}`)
}
return {
code: 0,
message: 'success',
data: {
id: data.id,
username: data.username,
display_name: data.display_name || data.username,
email: data.email || '',
business_line: data.business_line || '',
},
}
2026-07-14 15:39:32 +08:00
},
}