'use client'; import { useActionState, useState } from 'react'; import { mfaUseEmailAction, mfaVerifyAction, resendCodeAction } from '@/lib/auth/actions'; import { idle } from '@/lib/auth/state'; import { Checkbox, CodeInput, Cooldown, Field, FormMessage, SubmitButton, TextInput } from '@/components/account/form'; type Method = 'totp' | 'email_code' | 'recovery'; export function MfaForm({ stage }: { stage: 'totp' | 'email_code' }) { const [state, action] = useActionState(mfaVerifyAction, idle); const [emailState, emailAction] = useActionState(mfaUseEmailAction, idle); const [resend, resendAction] = useActionState(resendCodeAction, idle); const [method, setMethod] = useState(emailState.ok ? 'email_code' : stage); const effective: Method = emailState.ok && method === 'totp' ? 'email_code' : method; const cooldown = typeof resend.data?.retryAfter === 'number' ? resend.data.retryAfter : resend.ok || emailState.ok ? 45 : 0; return (
{effective === 'recovery' ? ( ) : ( )} Verify
{stage === 'totp' && effective !== 'email_code' ? (
E-mail me a code instead
) : null} {effective === 'email_code' ? (
{(ready, left) => ( {ready ? 'Send a new code' : `New code in ${left}s`} )} ) : null} {stage === 'totp' ? ( ) : null}
); }