{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "auth-03",
  "description": "A six-digit authenticator code form with a recovery code option, the tries left and a choice to trust the device.",
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/announcements.json",
    "button",
    "input",
    "switch",
    "https://www.dashboardblocks.com/r/radix/auth.json",
    "https://www.dashboardblocks.com/r/radix/rolling-text.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/auth/auth-03.tsx",
      "content": "'use client'\n\nimport {\n  Announcements,\n  useAnnouncements,\n} from '@/registry/components/dashboardblocks/announcements'\nimport {\n  AuthIcon,\n  AuthLayout,\n  AuthReveal,\n  AuthSwitch,\n  OtpInput,\n} from '@/registry/components/dashboardblocks/auth'\nimport { RollingText } from '@/registry/components/dashboardblocks/rolling-text'\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport {\n  type FormEvent,\n  startTransition,\n  useEffect,\n  useId,\n  useRef,\n  useState,\n} from 'react'\n\nimport { Button } from '@/components/ui/button'\nimport { Input } from '@/components/ui/input'\nimport { Switch } from '@/components/ui/switch'\n\ninterface VerifyResult {\n  ok: boolean\n  attemptsLeft?: number\n}\n\ntype Stage = 'code' | 'checking' | 'locked' | 'verified'\n\ninterface Auth3Props {\n  email: string\n  focusOnMount?: boolean\n  onVerified?: () => void\n  onVerify?: (\n    code: string,\n    options: { kind: 'app' | 'recovery'; trustDevice: boolean },\n  ) => VerifyResult | Promise<VerifyResult>\n  signOutHref: string\n  trustDays?: number\n}\n\nconst exampleProps: Auth3Props = {\n  email: 'amara@acme.co',\n  signOutHref: '#',\n  trustDays: 30,\n}\n\nconst EXAMPLE_ATTEMPTS = 5\n\nconst Auth3 = (props: Auth3Props) => {\n  const {\n    email,\n    focusOnMount = false,\n    onVerified,\n    onVerify,\n    signOutHref,\n    trustDays,\n  } = props\n  const id = useId()\n  const [kind, setKind] = useState<'app' | 'recovery'>('app')\n  const [code, setCode] = useState('')\n  const [recovery, setRecovery] = useState('')\n  const [trustDevice, setTrustDevice] = useState(false)\n  const [stage, setStage] = useState<Stage>('code')\n  const [error, setError] = useState<string | null>(null)\n  const [said, announce] = useAnnouncements()\n  const checking = stage === 'checking'\n  const verified = stage === 'verified'\n  const [exampleAttempts, setExampleAttempts] = useState(EXAMPLE_ATTEMPTS)\n  const [failures, setFailures] = useState(0)\n  const formRef = useRef<HTMLFormElement>(null)\n\n  useEffect(() => {\n    if (failures && !checking)\n      formRef.current?.querySelector<HTMLElement>('[aria-invalid=\"true\"]')?.focus()\n  }, [failures, checking])\n\n  const fail = (message: string) => {\n    setError(message)\n    setFailures((count) => count + 1)\n  }\n\n  const tryExampleCode = (value: string): VerifyResult => {\n    const ok = kind === 'app' ? value === '123456' : /^\\w{4}-\\w{4}$/.test(value)\n    const left = ok ? exampleAttempts : Math.max(0, exampleAttempts - 1)\n    setExampleAttempts(left)\n    return { attemptsLeft: left, ok }\n  }\n\n  const verify = async (value: string) => {\n    if (stage !== 'code') return\n    if (kind === 'app' && !/^\\d{6}$/.test(value))\n      return fail('Enter all 6 digits from your authenticator app.')\n    if (kind === 'recovery' && !value.trim()) return fail('Enter a recovery code.')\n    setStage('checking')\n    setError(null)\n    try {\n      const result = onVerify\n        ? await onVerify(value.trim(), { kind, trustDevice })\n        : tryExampleCode(value.trim())\n      startTransition(() => {\n        if (result.ok) {\n          setStage('verified')\n          announce('Signed in')\n          onVerified?.()\n        } else if (result.attemptsLeft === 0) {\n          setStage('locked')\n          setError('Too many tries. Wait 15 minutes, then try again.')\n        } else {\n          setStage('code')\n          setCode('')\n          fail(\n            kind === 'app'\n              ? `That code didn’t work.${result.attemptsLeft ? ` ${result.attemptsLeft} tries left.` : ''} Codes change every 30 seconds, so use the newest one.`\n              : 'That recovery code didn’t work, or it has been used.',\n          )\n        }\n      })\n    } catch {\n      startTransition(() => {\n        setStage('code')\n        fail('We couldn’t check the code. Check your connection and try again.')\n      })\n    }\n  }\n\n  const submit = (event: FormEvent<HTMLFormElement>) => {\n    event.preventDefault()\n    void verify(kind === 'app' ? code : recovery)\n  }\n\n  return (\n    <AuthLayout\n      media={\n        verified ? (\n          <AuthIcon tone='done' />\n        ) : (\n          <AuthIcon>\n            <IconPlaceholder\n              lucide='SmartphoneIcon'\n              tabler='IconDeviceMobile'\n              hugeicons='SmartPhone01Icon'\n              phosphor='DeviceMobileIcon'\n              remixicon='RiSmartphoneLine'\n              aria-hidden\n            />\n          </AuthIcon>\n        )\n      }\n      title={\n        <RollingText\n          text={verified ? 'You’re signed in' : 'Two-factor authentication'}\n          wrap\n        />\n      }\n      description={\n        <RollingText\n          wrap\n          text={\n            verified\n              ? 'Taking you to your dashboards…'\n              : kind === 'app'\n                ? 'Enter the 6-digit code from your authenticator app.'\n                : 'Enter one of the recovery codes you saved when you set up two-factor.'\n          }\n        />\n      }\n    >\n      <AuthReveal open={!verified}>\n        <div className='flex flex-col gap-6'>\n          <form\n            method='post'\n            ref={formRef}\n            noValidate\n            onSubmit={submit}\n            className='flex flex-col gap-4'\n          >\n            {kind === 'app' ? (\n              <OtpInput\n                aria-label='Code from your authenticator app'\n                aria-describedby={error ? `${id}-error` : undefined}\n                focusOnMount={focusOnMount}\n                value={code}\n                onChange={(next) => {\n                  setCode(next)\n                  setError(null)\n                }}\n                onComplete={(next) => void verify(next)}\n                invalid={Boolean(error)}\n                disabled={stage !== 'code'}\n              />\n            ) : (\n              <div className='flex flex-col gap-2'>\n                <label htmlFor={`${id}-recovery`} className='text-sm font-medium'>\n                  Recovery code\n                </label>\n                <Input\n                  id={`${id}-recovery`}\n                  autoComplete='off'\n                  spellCheck={false}\n                  placeholder='xxxx-xxxx'\n                  value={recovery}\n                  onChange={(event) => {\n                    setRecovery(event.target.value)\n                    setError(null)\n                  }}\n                  aria-invalid={error ? true : undefined}\n                  aria-describedby={error ? `${id}-error` : undefined}\n                  disabled={stage !== 'code'}\n                  className='font-mono'\n                />\n              </div>\n            )}\n            {error && (\n              <p id={`${id}-error`} role='alert' className='text-destructive text-sm'>\n                {error}\n              </p>\n            )}\n            {trustDays !== undefined && (\n              <div className='flex items-center justify-between gap-4 border-y py-3'>\n                <span id={`${id}-trust`} className='text-sm'>\n                  Trust this device for {trustDays} days\n                </span>\n                <Switch\n                  aria-labelledby={`${id}-trust`}\n                  checked={trustDevice}\n                  onCheckedChange={setTrustDevice}\n                />\n              </div>\n            )}\n            <Button type='submit' disabled={stage !== 'code'}>\n              {checking ? 'Checking' : 'Verify'}\n            </Button>\n            <Button\n              type='button'\n              variant='ghost'\n              disabled={checking}\n              onClick={() => {\n                setKind(kind === 'app' ? 'recovery' : 'app')\n                setError(null)\n              }}\n            >\n              {kind === 'app'\n                ? 'Use a recovery code instead'\n                : 'Use your authenticator app'}\n            </Button>\n          </form>\n          <AuthSwitch\n            href={signOutHref}\n            title='Use another account'\n            description={`Sign out of ${email} first.`}\n          />\n        </div>\n      </AuthReveal>\n      <Announcements said={said} />\n    </AuthLayout>\n  )\n}\n\nexport { Auth3, exampleProps as auth3ExampleProps, type Auth3Props, type VerifyResult }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/auth/auth-03.tsx"
    }
  ],
  "meta": {
    "shows": "A six-digit authenticator code form with a recovery code option, the tries left and a choice to trust the device.",
    "size": "page",
    "usedWith": [
      "auth-04"
    ],
    "component": "Auth3",
    "props": "{ email: string; focusOnMount?: boolean; onVerified?: () => void; onVerify?: (code: string, options: { kind: \"app\" | \"recovery\"; trustDevice: boolean }) => { ok: boolean; attemptsLeft?: number } | Promise<{ ok: boolean; attemptsLeft?: number }>; signOutHref: string; trustDays?: number }"
  },
  "type": "registry:component"
}
