"use client"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { createApiClient } from "@app/lib/api"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { AxiosResponse } from "axios"; import { Disable2faBody, Disable2faResponse } from "@server/routers/auth"; import { z } from "zod"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@app/components/ui/form"; import { Credenza, CredenzaBody, CredenzaClose, CredenzaContent, CredenzaDescription, CredenzaFooter, CredenzaHeader, CredenzaTitle } from "@app/components/Credenza"; import { toast } from "@app/hooks/useToast"; import { formatAxiosError } from "@app/lib/api";; import { useUserContext } from "@app/hooks/useUserContext"; import { InputOTP, InputOTPGroup, InputOTPSlot } from "./ui/input-otp"; import { REGEXP_ONLY_DIGITS_AND_CHARS } from "input-otp"; import { CheckCircle2 } from "lucide-react"; const disableSchema = z.object({ password: z.string().min(1, { message: "Password is required" }), code: z.string().min(1, { message: "Code is required" }) }); type Disable2FaProps = { open: boolean; setOpen: (val: boolean) => void; }; export default function Disable2FaForm({ open, setOpen }: Disable2FaProps) { const [loading, setLoading] = useState(false); const [step, setStep] = useState<"password" | "success">("password"); const { user, updateUser } = useUserContext(); const api = createApiClient(useEnvContext()); const disableForm = useForm>({ resolver: zodResolver(disableSchema), defaultValues: { password: "", code: "" } }); const request2fa = async (values: z.infer) => { setLoading(true); const res = await api .post>(`/auth/2fa/disable`, { password: values.password, code: values.code } as Disable2faBody) .catch((e) => { toast({ title: "Unable to disable 2FA", description: formatAxiosError( e, "An error occurred while disabling 2FA" ), variant: "destructive" }); }); if (res) { // toast({ // title: "Two-factor disabled", // description: // "Two-factor authentication has been disabled for your account" // }); updateUser({ twoFactorEnabled: false }); setStep("success"); } setLoading(false); }; function reset() { disableForm.reset(); setStep("password"); setLoading(false); } return ( { setOpen(val); reset(); }} > Disable Two-factor Authentication Disable two-factor authentication for your account {step === "password" && (
( Password )} /> ( Authenticator Code )} />
)} {step === "success" && (

Two-Factor Authentication Disabled

Two-factor authentication has been disabled for your account. You can enable it again at any time.

)}
{step === "password" && ( )}
); }