import { getApiUrl } from "@/lib/config"; type ApiError = { error?: string; message?: string }; async function parseError(res: Response) { const data = (await res.json().catch(() => ({}))) as ApiError; return data.error ?? data.message ?? "Something went wrong"; } export async function registerAccount(input: { firstName: string; lastName: string; email: string; password: string; }) { const res = await fetch(`${getApiUrl()}/api/auth/register`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(input), }); if (!res.ok) { throw new Error(await parseError(res)); } } export async function requestPasswordReset(email: string) { const res = await fetch(`${getApiUrl()}/api/auth/forgot-password`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email }), }); if (!res.ok) { throw new Error(await parseError(res)); } const data = (await res.json()) as { message?: string }; return data.message ?? "Check your email for reset instructions."; } export async function resetPassword(token: string, password: string) { const res = await fetch(`${getApiUrl()}/api/auth/reset-password`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ token, password }), }); if (!res.ok) { throw new Error(await parseError(res)); } }