import { useEffect } from 'react' import { Form, Formik } from 'formik' import { trpc } from '~/utils/trpc' import IVButton from '~/components/IVButton' import IVSpinner from '~/components/IVSpinner' import MFAInput from '../MFAInput' export default function EnrollMFAForm({ onSubmit }: { onSubmit: () => void }) { const start = trpc.useMutation(['auth.mfa.enroll.start']) const complete = trpc.useMutation(['auth.mfa.enroll.complete'], { onSuccess() { onSubmit() }, }) const { mutate: startEnrollment } = start useEffect(() => { startEnrollment() }, [startEnrollment]) return ( key={start.data?.challengeId} initialValues={{ code: '', }} onSubmit={({ code }, { setFieldValue }) => { if (!start.data?.challengeId) return complete.mutate( { code, challengeId: start.data.challengeId, }, { onError: () => { setFieldValue('code', '') }, } ) }} >

Scan this QR code or use the secret below with your authenticator app of choice. Enter the verification code it gives you to complete enrollment.

{start.data ? ( <> QR code to enroll in MFA, alternatively use secret below

{start.data.secret}

) : ( )}
{complete.isError && (
Sorry, that code is invalid. Please try again.
)}
) }