import { Alpine } from "./alpine"; import { authHeaders, removeToken } from "./storage"; import { showToast } from "./toast"; async function confirmDeleteAccount(): Promise { if ( !confirm( "Are you sure? All preferences and devices will be deleted. This action cannot be undone.", ) ) { return; } // This page is SSR-gated — reaching this script means authed (the // middleware would have redirected). A localStorage-token check // wrongly bounced cookie-authed users to /login (2026-10-31). try { const response = await fetch("/api/auth/profile", { method: "DELETE", headers: authHeaders(), }); if (response.ok) { removeToken(); localStorage.removeItem("user"); window.location.href = "/login?deleted=true"; } else { const error = await response.json(); showToast(error.error || "Failed to delete account", "error"); } } catch (error) { console.error("Failed to delete account", error); showToast("Failed to delete account", "error"); } } export { confirmDeleteAccount }; Alpine.data("profile", () => ({ confirmDeleteAccount, }));