61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
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<T>(response: Response): Promise<T> {
|
||
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<AuthenticationSettings> {
|
||
return readResponse(await apiFetch(`${getBackendBaseURL()}/api/system-settings/authentication`));
|
||
}
|
||
|
||
export async function saveAuthenticationSettings(settings: AuthenticationSettings): Promise<AuthenticationSettings> {
|
||
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<AccountPasswordStatus> {
|
||
return readResponse(await apiFetch(`${getBackendBaseURL()}/api/v1/auth/me`));
|
||
}
|
||
|
||
export async function changeAccountPassword(currentPassword: string, newPassword: string): Promise<PasswordSession> {
|
||
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 }),
|
||
}));
|
||
}
|