import { apiFetch } from "@/core/api/fetch-client"; import { getBackendBaseURL } from "@/core/config"; export interface AuthenticationSettings { password_login_enabled: boolean; registration_enabled: boolean; sso_enabled: boolean; single_session_enabled: boolean; } export interface PasswordSession { access_token?: string; token_type?: string; expires_in?: number; } export interface AccountPasswordStatus { id: string; has_password: boolean; requires_current_password: boolean; uses_default_password: boolean; } async function readResponse(response: Response): Promise { const data = await response.json().catch(() => null); if (!response.ok) { const detail = data?.detail; const message = typeof detail === "string" ? detail : Array.isArray(detail) ? detail.map((item: { msg?: string }) => item.msg).filter(Boolean).join(";") : detail?.message; throw new Error(message || `请求失败(${response.status})`); } return data as T; } export async function getAuthenticationSettings(): Promise { return readResponse(await apiFetch(`${getBackendBaseURL()}/api/system-settings/authentication`)); } export async function saveAuthenticationSettings(settings: AuthenticationSettings): Promise { return readResponse(await apiFetch(`${getBackendBaseURL()}/api/system-settings/authentication`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(settings), })); } export async function getAccountPasswordStatus(): Promise { return readResponse(await apiFetch(`${getBackendBaseURL()}/api/v1/auth/me`)); } export async function changeAccountPassword(currentPassword: string, newPassword: string): Promise { return readResponse(await apiFetch(`${getBackendBaseURL()}/api/v1/auth/change-password`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ current_password: currentPassword, new_password: newPassword }), })); }