import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { api } from "@/utils/api"; import { zodResolver } from "@hookform/resolvers/zod"; import { AlertTriangle, Fingerprint } from "lucide-react"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; import { InputOTP, InputOTPGroup, InputOTPSlot, } from "@/components/ui/input-otp"; const Enable2FASchema = z.object({ pin: z.string().min(6, { message: "Pin is required", }), }); type Enable2FA = z.infer; export const Enable2FA = () => { const utils = api.useUtils(); const { data } = api.auth.generate2FASecret.useQuery(undefined, { refetchOnWindowFocus: false, }); const { mutateAsync, isLoading, error, isError } = api.auth.verify2FASetup.useMutation(); const form = useForm({ defaultValues: { pin: "", }, resolver: zodResolver(Enable2FASchema), }); useEffect(() => { form.reset({ pin: "", }); }, [form, form.reset, form.formState.isSubmitSuccessful]); const onSubmit = async (formData: Enable2FA) => { await mutateAsync({ pin: formData.pin, secret: data?.secret || "", }) .then(async () => { toast.success("2FA Verified"); utils.auth.get.invalidate(); }) .catch(() => { toast.error("Error to verify the 2FA"); }); }; return ( 2FA Setup Add a 2FA to your account {isError && (
{error?.message}
)}
{data?.qrCodeUrl ? "Scan the QR code to add 2FA" : ""} qrCode
{data?.secret ? `Secret: ${data?.secret}` : ""}
( Pin Please enter the 6 digits code provided by your authenticator app. )} />
); };