"use client"; import { useState } from "react"; import { useTranslations } from "next-intl"; import { useRouter } from "next/navigation"; import { AlertCircle, Loader2, TriangleAlert } from "lucide-react"; import { authClient } from "@/lib/auth-client"; import { track } from "@/lib/datafast"; import { resetAnalytics } from "@/lib/posthog"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; interface DeleteAccountSectionProps { email: string; // An App Store subscription keeps billing until it's cancelled in the App Store. hasAppStoreSubscription?: boolean; } export function DeleteAccountSection({ email, hasAppStoreSubscription = false }: DeleteAccountSectionProps) { const t = useTranslations("settings"); const common = useTranslations("common"); const [open, setOpen] = useState(false); const [confirmation, setConfirmation] = useState(""); const [password, setPassword] = useState(""); const [isDeleting, setIsDeleting] = useState(false); const [error, setError] = useState(null); const router = useRouter(); const confirmed = confirmation.trim().toLowerCase() === email.trim().toLowerCase(); const handleOpenChange = (next: boolean) => { if (isDeleting) return; setOpen(next); if (!next) { setConfirmation(""); setPassword(""); setError(null); } }; const handleDelete = async (e: React.FormEvent) => { e.preventDefault(); if (!confirmed || !password || isDeleting) return; setIsDeleting(true); setError(null); try { const response = await authClient.deleteUser({ password }); if (response.error) { setError(response.error.message || t("deleteAccount.failed")); setIsDeleting(false); return; } } catch (deleteError) { setError(deleteError instanceof Error ? deleteError.message : t("deleteAccount.failed")); setIsDeleting(false); return; } track("account_deleted"); // The server already revoked every session; this clears any client state. await authClient.signOut().catch(() => undefined); resetAnalytics(); router.push("/"); router.refresh(); }; return (

{t("deleteAccount.title")}

{t("deleteAccount.description")}

{t("deleteAccount.label")}

{t("deleteAccount.hint")}

{t("deleteAccount.dialogTitle")} {t("deleteAccount.dialogBody")} {hasAppStoreSubscription && ( {t("deleteAccount.appStore")} )}
setConfirmation(e.target.value)} autoComplete="off" disabled={isDeleting} />
setPassword(e.target.value)} autoComplete="current-password" disabled={isDeleting} />
{error && ( {error} )}
); }