import { useEffect } from 'react' import { Helmet } from 'react-helmet-async' import { Field, Form, Formik } from 'formik' import { Navigate } from 'react-router-dom' import { trpc } from '~/utils/trpc' import IVInputField from '~/components/IVInputField' import IVButton from '~/components/IVButton' import { logout } from '~/utils/auth' import useHasSession from '~/utils/useHasSession' import AuthPageHeader from '~/components/AuthPageHeader' import MFAInput from '~/components/MFAInput' export default function ValidateMfaPage() { const session = trpc.useQuery(['auth.session.user']) const { hasSession, isLoading: isSessionLoading, needsMfa } = useHasSession() const challenge = trpc.useMutation(['auth.mfa.challenge']) const verify = trpc.useMutation(['auth.mfa.verify'], { onSuccess() { window.location.assign('/dashboard') }, }) const { mutate: challengeMfa } = challenge useEffect(() => { if (!hasSession && needsMfa && !isSessionLoading) { challengeMfa() } }, [challengeMfa, hasSession, needsMfa, isSessionLoading]) if (hasSession) { return } if (!needsMfa && !isSessionLoading) { return } return (
Log in | Interval
initialValues={{ code: '', }} onSubmit={async ({ code }) => { if (!challenge.data) return verify.mutate({ code, challengeId: challenge.data, }) }} validate={({ code }) => { if (!code) { return { code: 'Please enter a code.', } } }} > {({ isValid }) => (
{verify.isError && (
Sorry, that code is invalid. Please try again.
)}
{ await logout() window.location.assign('/login') }} />
)}
) }