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

231 lines
5.8 KiB
TypeScript
Raw Normal View History

2025-12-10 20:52:44 +08:00
import apiClient from './client'
import { log } from '@/utils/logger'
2025-12-10 20:52:44 +08:00
export interface LoginRequest {
email: string
password: string
2026-01-02 16:17:24 +08:00
auth_type?: 'local' | 'ldap'
2025-12-10 20:52:44 +08:00
}
export interface LoginResponse {
access_token: string
token_type?: string
expires_in?: number
user_id?: string // UUID
email?: string
username?: string
role?: string
}
export interface UserPreferences {
theme?: 'light' | 'dark' | 'auto'
language?: string
notifications_enabled?: boolean
[key: string]: unknown // 允许扩展其他偏好设置
}
export interface UserStats {
total_requests?: number
total_cost?: number
last_request_at?: string
[key: string]: unknown // 允许扩展其他统计数据
}
2025-12-30 17:15:48 +08:00
export interface SendVerificationCodeRequest {
email: string
2026-05-15 01:54:20 +08:00
turnstile_token?: string
2025-12-30 17:15:48 +08:00
}
export interface SendVerificationCodeResponse {
message: string
success: boolean
expire_minutes?: number
verification_token: string
2025-12-30 17:15:48 +08:00
}
export interface VerifyEmailRequest {
email: string
code: string
verification_token: string
2025-12-30 17:15:48 +08:00
}
export interface VerifyEmailResponse {
message: string
success: boolean
}
export interface VerificationStatusRequest {
email: string
verification_token: string
}
export interface VerificationStatusResponse {
email: string
has_pending_code: boolean
is_verified: boolean
cooldown_remaining: number | null
code_expires_in: number | null
}
2025-12-30 17:15:48 +08:00
export interface RegisterRequest {
email?: string
2025-12-30 17:15:48 +08:00
username: string
password: string
2026-05-15 01:54:20 +08:00
turnstile_token?: string
invite_code?: string
privacy_policy_accepted?: boolean
privacy_policy_version?: string
email_verification_token?: string
2025-12-30 17:15:48 +08:00
}
export interface RegisterResponse {
user_id: string
email?: string
2025-12-30 17:15:48 +08:00
username: string
message: string
}
export interface RegistrationSettingsResponse {
enable_registration: boolean
require_email_verification: boolean
email_configured: boolean
password_policy_level: string
2026-05-15 01:54:20 +08:00
turnstile_enabled?: boolean
turnstile_site_key?: string | null
turnstile_required_actions?: string[]
privacy_policy?: RegistrationPrivacyPolicySettings
}
export interface RegistrationPrivacyPolicySettings {
enabled: boolean
format: 'markdown' | 'html'
content: string
version: string
2025-12-30 17:15:48 +08:00
}
2026-01-02 16:17:24 +08:00
export interface AuthSettingsResponse {
local_enabled: boolean
ldap_enabled: boolean
ldap_exclusive: boolean
}
export interface BillingSummary {
id?: string | null
balance: number
recharge_balance: number
gift_balance: number
refundable_balance: number
currency: string
status: string
limit_mode: 'finite' | 'unlimited'
unlimited: boolean
total_recharged: number
total_consumed: number
total_refunded: number
total_adjusted: number
updated_at?: string | null
}
2025-12-10 20:52:44 +08:00
export interface User {
id: string // UUID
username: string
email?: string
role: string // 'admin' or 'user'
is_active: boolean
billing?: BillingSummary
2025-12-10 20:52:44 +08:00
allowed_providers?: string[] | null // 允许使用的提供商 ID 列表
2026-01-07 19:53:32 +08:00
allowed_api_formats?: string[] | null // 允许使用的 API 格式列表
2025-12-10 20:52:44 +08:00
allowed_models?: string[] | null // 允许使用的模型名称列表
created_at: string
last_login_at?: string
preferences?: UserPreferences
stats?: UserStats
}
export const authApi = {
async login(credentials: LoginRequest): Promise<LoginResponse> {
const response = await apiClient.post<LoginResponse>('/api/auth/login', credentials)
apiClient.setToken(response.data.access_token, true)
2025-12-10 20:52:44 +08:00
return response.data
},
async logout() {
try {
// 调用后端登出接口,将 Token 加入黑名单
await apiClient.post('/api/auth/logout', {})
} catch (error) {
// 即使后端登出失败,也要清除本地认证信息
log.warn('后端登出失败,仅清除本地认证信息:', error)
2025-12-10 20:52:44 +08:00
} finally {
// 清除本地认证信息
apiClient.clearAuth()
}
},
async getCurrentUser(): Promise<User> {
const response = await apiClient.get<User>('/api/users/me')
return response.data
},
async refreshToken(): Promise<LoginResponse> {
2026-05-27 15:06:51 +08:00
const response = await apiClient.post<LoginResponse>('/api/auth/refresh')
2025-12-10 20:52:44 +08:00
apiClient.setToken(response.data.access_token)
return response.data
2025-12-30 17:15:48 +08:00
},
2026-05-15 01:54:20 +08:00
async sendVerificationCode(
email: string,
turnstileToken?: string
): Promise<SendVerificationCodeResponse> {
const payload: SendVerificationCodeRequest = { email }
if (turnstileToken) {
payload.turnstile_token = turnstileToken
}
2025-12-30 17:15:48 +08:00
const response = await apiClient.post<SendVerificationCodeResponse>(
'/api/auth/send-verification-code',
2026-05-15 01:54:20 +08:00
payload
2025-12-30 17:15:48 +08:00
)
return response.data
},
async verifyEmail(
email: string,
code: string,
verificationToken: string
): Promise<VerifyEmailResponse> {
2025-12-30 17:15:48 +08:00
const response = await apiClient.post<VerifyEmailResponse>(
'/api/auth/verify-email',
{ email, code, verification_token: verificationToken }
2025-12-30 17:15:48 +08:00
)
return response.data
},
async register(data: RegisterRequest): Promise<RegisterResponse> {
const response = await apiClient.post<RegisterResponse>('/api/auth/register', data)
return response.data
},
async getRegistrationSettings(): Promise<RegistrationSettingsResponse> {
const response = await apiClient.get<RegistrationSettingsResponse>(
'/api/auth/registration-settings'
)
return response.data
},
async getVerificationStatus(
email: string,
verificationToken: string
): Promise<VerificationStatusResponse> {
const response = await apiClient.post<VerificationStatusResponse>(
'/api/auth/verification-status',
{ email, verification_token: verificationToken }
)
return response.data
2026-01-02 16:17:24 +08:00
},
async getAuthSettings(): Promise<AuthSettingsResponse> {
const response = await apiClient.get<AuthSettingsResponse>('/api/auth/settings')
return response.data
2025-12-10 20:52:44 +08:00
}
}