import { requestJson, setCsrfToken } from "./http"; import type { AuthUser } from "./types"; interface AuthResponse { user: AuthUser; csrfToken: string | null; } function applyAuthResponse(response: AuthResponse): AuthResponse { setCsrfToken(response.csrfToken); return response; } export async function getCurrentUser(): Promise { return applyAuthResponse(await requestJson("/auth/me")); } export async function register(payload: { email: string; password: string; turnstileToken: string; }): Promise { await requestJson("/auth/register", { method: "POST", body: JSON.stringify(payload), }); } export async function login(payload: { email: string; password: string }): Promise { return applyAuthResponse( await requestJson("/auth/login", { method: "POST", body: JSON.stringify(payload), }), ); } export async function logout(): Promise { await requestJson("/auth/logout", { method: "POST" }); setCsrfToken(null); } export async function verifyEmail(token: string): Promise { await requestJson("/auth/verify-email", { method: "POST", body: JSON.stringify({ token }), }); } export async function resendVerification(email: string): Promise { await requestJson("/auth/resend-verification", { method: "POST", body: JSON.stringify({ email }), }); } export async function forgotPassword(email: string): Promise { await requestJson("/auth/forgot-password", { method: "POST", body: JSON.stringify({ email }), }); } export async function resetPassword(token: string, password: string): Promise { await requestJson("/auth/reset-password", { method: "POST", body: JSON.stringify({ token, password }), }); }