import { zodResolver } from "@hookform/resolvers/zod"; import { t } from "@lingui/core/macro"; import { Trans } from "@lingui/react/macro"; import { EyeIcon, EyeSlashIcon, LockOpenIcon } from "@phosphor-icons/react"; import { useRouter } from "@tanstack/react-router"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { useToggle } from "usehooks-ts"; import z from "zod"; import { Button } from "@/components/ui/button"; import { DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { useFormBlocker } from "@/hooks/use-form-blocker"; import { authClient } from "@/integrations/auth/client"; import { getReadableErrorMessage } from "@/utils/error-message"; import { type DialogProps, useDialogStore } from "../store"; const formSchema = z.object({ password: z.string().min(6).max(64), }); type FormValues = z.infer; export function DisableTwoFactorDialog(_: DialogProps<"auth.two-factor.disable">) { const router = useRouter(); const [showPassword, toggleShowPassword] = useToggle(false); const closeDialog = useDialogStore((state) => state.closeDialog); const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { password: "", }, }); const { blockEvents } = useFormBlocker(form); const onSubmit = async (data: FormValues) => { const toastId = toast.loading(t`Disabling two-factor authentication...`); const { error } = await authClient.twoFactor.disable({ password: data.password }); if (error) { toast.error( getReadableErrorMessage( error, t({ comment: "Fallback toast when disabling two-factor authentication fails", message: "Failed to disable two-factor authentication. Please try again.", }), ), { id: toastId }, ); return; } toast.success(t`Two-factor authentication has been disabled successfully.`, { id: toastId }); void router.invalidate(); closeDialog(); form.reset(); }; return ( Disable Two-Factor Authentication Enter your password to disable two-factor authentication. Your account will be less secure without 2FA enabled.
( Password
} />
)} />
); }