import { apiFetch, apiFetchBlob, apiFetchFormData } from "@/lib/auth/api-client"; import { MfaEnrollment, ProfileSummary, UpdateProfileInput } from "./types"; export function updateProfile(input: UpdateProfileInput): Promise { return apiFetch("/profile", { method: "PATCH", body: JSON.stringify(input), }); } export function changePassword(currentPassword: string, newPassword: string): Promise { return apiFetch("/profile/password", { method: "POST", body: JSON.stringify({ currentPassword, newPassword }), }); } export function uploadAvatar(file: File): Promise { const formData = new FormData(); formData.append("file", file); return apiFetchFormData("/profile/avatar", formData); } export function deleteAvatar(): Promise { return apiFetch("/profile/avatar", { method: "DELETE" }); } // Fetches the avatar as a blob (the endpoint requires the caller's own // Bearer token, so a plain can't hit it directly) and hands // back an object URL for use as an source. Returns null when the user // has no avatar (404) or the request otherwise fails. export async function fetchAvatarUrl(): Promise { try { const { blob } = await apiFetchBlob("/profile/avatar", "avatar"); return URL.createObjectURL(blob); } catch { return null; } } export function enrollMfa(): Promise { return apiFetch("/profile/mfa/enroll", { method: "POST" }); } export function confirmMfaEnrollment(setupToken: string, code: string): Promise { return apiFetch("/profile/mfa/enroll/confirm", { method: "POST", body: JSON.stringify({ setupToken, code }), }); } export function disableMfa(currentPassword: string): Promise { return apiFetch("/profile/mfa/disable", { method: "POST", body: JSON.stringify({ currentPassword }), }); }