"use client"; import { useState } from "react"; import { toast } from "sonner"; import { Button, buttonVariants } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { useTranslations } from "next-intl"; import { authClient } from "@/lib/auth/client"; import { TwoFactorSettings } from "@/components/settings/two-factor-settings"; export function SecurityForm({ currentEmail, twoFactorEnabled, hasPassword, }: { currentEmail: string; twoFactorEnabled: boolean; hasPassword: boolean; }) { const t = useTranslations("settingsForm"); const [newEmail, setNewEmail] = useState(""); const [sendingEmail, setSendingEmail] = useState(false); const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [changingPassword, setChangingPassword] = useState(false); async function handleChangeEmail(e: React.FormEvent) { e.preventDefault(); if (!newEmail.trim()) return; setSendingEmail(true); try { const { error } = await authClient.changeEmail({ newEmail: newEmail.trim(), callbackURL: "/settings", }); if (error) { toast.error(error.message ?? t("emailChangeFailed")); } else { toast.success(t("emailChangeSent")); setNewEmail(""); } } finally { setSendingEmail(false); } } async function handleChangePassword(e: React.FormEvent) { e.preventDefault(); if (newPassword !== confirmPassword) { toast.error(t("passwordMismatch")); return; } setChangingPassword(true); try { const { error } = await authClient.changePassword({ currentPassword, newPassword, revokeOtherSessions: false, }); if (error) { toast.error(error.message ?? t("passwordChangeFailed")); } else { toast.success(t("passwordChanged")); setCurrentPassword(""); setNewPassword(""); setConfirmPassword(""); } } finally { setChangingPassword(false); } } return (
{t("changeEmailDescription")}
{t("downloadDataDescription")}