{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "otp-two-factor",
  "type": "registry:block",
  "title": "OTP Two-Factor",
  "description": "Authenticator two-factor prompt with trust-this-device and a backup code.",
  "files": [
    {
      "path": "src/registry/blocks/auth/otp-two-factor.tsx",
      "type": "registry:block",
      "content": "\"use client\";\n\nimport {\n  useEffect,\n  useId,\n  useRef,\n  useState,\n  type ComponentProps,\n  type CSSProperties,\n  type FormEvent,\n} from \"react\";\nimport { REGEXP_ONLY_DIGITS } from \"input-otp\";\nimport { Loader2 } from \"lucide-react\";\n\nimport { Button } from \"@/components/ui/button\";\nimport { Checkbox } from \"@/components/ui/checkbox\";\nimport { Input } from \"@/components/ui/input\";\nimport {\n  InputOTP,\n  InputOTPGroup,\n  InputOTPSeparator,\n  InputOTPSlot,\n} from \"@/components/ui/input-otp\";\nimport { Label } from \"@/components/ui/label\";\nimport { cn } from \"@/lib/utils\";\n\nexport type TwoFactorMethod = \"authenticator\" | \"backup\";\n\nexport type OtpTwoFactorValues = {\n  method: TwoFactorMethod;\n  /** Six digits for the authenticator, or the 8-character backup code (upper case, no hyphen). */\n  code: string;\n  trustDevice: boolean;\n};\n\nexport type OtpTwoFactorProps = {\n  /** Product name shown in the wordmark. */\n  brand?: string;\n  title?: string;\n  description?: string;\n  /** Signed-in account, shown in mono. */\n  account?: string;\n  /** Number of days \"Trust this device\" lasts. */\n  trustDays?: number;\n  supportHref?: string;\n  signInHref?: string;\n  continueHref?: string;\n  /** Called with the method, code and trust choice. Throw to show an error. */\n  onSubmit?: (values: OtpTwoFactorValues) => void | Promise<void>;\n  /** Panel ink. Defaults to a pale blue tint. */\n  panel?: string;\n  /** Text colour on `panel`. Defaults to near-black. */\n  panelForeground?: string;\n  /** Wordmark square and button ink. Defaults to Devsloka blue. */\n  accent?: string;\n  /** Text colour on `accent`. */\n  accentForeground?: string;\n} & Omit<ComponentProps<\"section\">, \"onSubmit\">;\n\nconst DEFAULTS = {\n  brand: \"Larch\",\n  title: \"Two-step verification\",\n  description: \"Open your authenticator app and enter the 6-digit code for Larch.\",\n  account: \"priya@fieldwork.org\",\n  panel: \"#E8F0FF\",\n  panelForeground: \"#0A0A0A\",\n  accent: \"#0A6EFF\",\n  accentForeground: \"#FFFFFF\",\n};\n\nconst CODE_LENGTH = 6;\nconst BACKUP_PATTERN = /^[A-Z0-9]{8}$/;\n\nconst normalizeBackup = (value: string) => value.replace(/[\\s-]/g, \"\").toUpperCase();\n\nfunction validate(method: TwoFactorMethod, code: string, backup: string) {\n  if (method === \"authenticator\") {\n    if (code.length < CODE_LENGTH) return `Enter all ${CODE_LENGTH} digits from your authenticator app.`;\n    return undefined;\n  }\n  const normalized = normalizeBackup(backup);\n  if (!normalized) return \"Enter one of your saved backup codes.\";\n  if (!BACKUP_PATTERN.test(normalized))\n    return \"Backup codes are 8 letters and numbers, like 4F7K-29QD.\";\n  return undefined;\n}\n\n/** Placeholder until you pass `onSubmit`: waits briefly, then succeeds. */\nconst defaultSubmit: (values: OtpTwoFactorValues) => Promise<void> = () =>\n  new Promise<void>((resolve) => setTimeout(resolve, 800));\n\nfunction AuthenticatorMark() {\n  return (\n    <svg viewBox=\"0 0 140 220\" aria-hidden=\"true\" className=\"h-44 w-auto md:h-56\">\n      {/* Body */}\n      <rect x=\"2\" y=\"2\" width=\"136\" height=\"216\" rx=\"14\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"4\" />\n      {/* Speaker */}\n      <rect x=\"54\" y=\"16\" width=\"32\" height=\"5\" rx=\"2.5\" fill=\"currentColor\" />\n      {/* Screen */}\n      <rect x=\"16\" y=\"34\" width=\"108\" height=\"152\" fill=\"currentColor\" opacity=\"0.12\" />\n      {/* Account row */}\n      <rect x=\"28\" y=\"54\" width=\"52\" height=\"6\" fill=\"currentColor\" />\n      <rect x=\"28\" y=\"66\" width=\"34\" height=\"4\" fill=\"currentColor\" opacity=\"0.5\" />\n      {/* Six digits, 3 + 3 */}\n      {[0, 1, 2].map((i) => (\n        <rect key={`a${i}`} x={28 + i * 14} y=\"88\" width=\"10\" height=\"22\" fill=\"currentColor\" />\n      ))}\n      {[0, 1, 2].map((i) => (\n        <rect key={`b${i}`} x={78 + i * 14} y=\"88\" width=\"10\" height=\"22\" fill=\"currentColor\" />\n      ))}\n      {/* Timer bar */}\n      <rect x=\"28\" y=\"124\" width=\"84\" height=\"4\" fill=\"currentColor\" opacity=\"0.3\" />\n      <rect x=\"28\" y=\"124\" width=\"52\" height=\"4\" fill=\"currentColor\" />\n      {/* Second account, dimmed */}\n      <rect x=\"28\" y=\"148\" width=\"44\" height=\"6\" fill=\"currentColor\" opacity=\"0.5\" />\n      <rect x=\"28\" y=\"160\" width=\"60\" height=\"10\" fill=\"currentColor\" opacity=\"0.3\" />\n      {/* Home bar */}\n      <rect x=\"50\" y=\"200\" width=\"40\" height=\"4\" rx=\"2\" fill=\"currentColor\" />\n    </svg>\n  );\n}\n\nexport function OtpTwoFactor({\n  brand = DEFAULTS.brand,\n  title = DEFAULTS.title,\n  description = DEFAULTS.description,\n  account = DEFAULTS.account,\n  trustDays = 30,\n  supportHref = \"#\",\n  signInHref = \"#\",\n  continueHref = \"#\",\n  onSubmit = defaultSubmit,\n  panel = DEFAULTS.panel,\n  panelForeground = DEFAULTS.panelForeground,\n  accent = DEFAULTS.accent,\n  accentForeground = DEFAULTS.accentForeground,\n  className,\n  style,\n  ...props\n}: OtpTwoFactorProps) {\n  const id = useId();\n  const titleId = `${id}-title`;\n  const codeId = `${id}-code`;\n  const backupId = `${id}-backup`;\n  const trustId = `${id}-trust`;\n  const errorId = `${id}-error`;\n  const hintId = `${id}-hint`;\n\n  const codeRef = useRef<HTMLInputElement>(null);\n  const backupRef = useRef<HTMLInputElement>(null);\n  const successHeadingRef = useRef<HTMLHeadingElement>(null);\n  const focusAfterSwitch = useRef(false);\n\n  const [method, setMethod] = useState<TwoFactorMethod>(\"authenticator\");\n  const [code, setCode] = useState(\"\");\n  const [backup, setBackup] = useState(\"\");\n  const [trustDevice, setTrustDevice] = useState(false);\n  const [error, setError] = useState<string>();\n  const [submitted, setSubmitted] = useState(false);\n  const [status, setStatus] = useState<\"idle\" | \"submitting\" | \"success\">(\"idle\");\n  const [announcement, setAnnouncement] = useState(\"\");\n\n  useEffect(() => {\n    if (status === \"success\") successHeadingRef.current?.focus();\n  }, [status]);\n\n  useEffect(() => {\n    if (!focusAfterSwitch.current) return;\n    focusAfterSwitch.current = false;\n    (method === \"authenticator\" ? codeRef : backupRef).current?.focus();\n  }, [method]);\n\n  const focusField = () => (method === \"authenticator\" ? codeRef : backupRef).current?.focus();\n\n  const switchMethod = () => {\n    focusAfterSwitch.current = true;\n    setMethod((current) => (current === \"authenticator\" ? \"backup\" : \"authenticator\"));\n    setError(undefined);\n    setSubmitted(false);\n    setAnnouncement(\"\");\n  };\n\n  const handleSubmit = async (event: FormEvent<HTMLFormElement>) => {\n    event.preventDefault();\n    if (status === \"submitting\") return;\n    setSubmitted(true);\n    const nextError = validate(method, code, backup);\n    setError(nextError);\n    if (nextError) {\n      focusField();\n      return;\n    }\n\n    setStatus(\"submitting\");\n    setAnnouncement(\"Checking the code…\");\n    try {\n      await onSubmit({\n        method,\n        code: method === \"authenticator\" ? code : normalizeBackup(backup),\n        trustDevice,\n      });\n      setStatus(\"success\");\n      setAnnouncement(\"Verified. You are signed in.\");\n    } catch {\n      setStatus(\"idle\");\n      setAnnouncement(\"\");\n      setError(\n        method === \"authenticator\"\n          ? \"That code didn’t work. Codes change every 30 seconds, so wait for the next one and try again.\"\n          : \"That backup code isn’t valid or has already been used. Each code works once.\"\n      );\n      focusField();\n    }\n  };\n\n  const describedBy = [hintId, error ? errorId : undefined].filter(Boolean).join(\" \");\n\n  return (\n    <section\n      data-slot=\"otp-two-factor\"\n      aria-labelledby={titleId}\n      style={\n        {\n          \"--block-panel\": panel,\n          \"--block-panel-fg\": panelForeground,\n          \"--block-accent\": accent,\n          \"--block-accent-fg\": accentForeground,\n          ...style,\n        } as CSSProperties\n      }\n      className={cn(\"w-full bg-background text-foreground\", className)}\n      {...props}\n    >\n      <div className=\"mx-auto max-w-6xl px-6 py-16 md:py-24\">\n        <div className=\"grid border md:grid-cols-12\">\n          <div className=\"flex flex-col justify-between gap-10 bg-[var(--block-panel)] px-6 py-8 text-[var(--block-panel-fg)] md:col-span-5 md:px-10 md:py-12\">\n            <div className=\"flex items-baseline justify-between gap-4\">\n              <span className=\"inline-flex items-center gap-2 text-base font-semibold tracking-tight\">\n                <span aria-hidden=\"true\" className=\"size-2.5 bg-[var(--block-accent)]\" />\n                {brand}\n              </span>\n              <span className=\"font-mono text-[11px] tracking-wider uppercase\">Step 2 / 2</span>\n            </div>\n            <AuthenticatorMark />\n            <p className=\"max-w-xs font-mono text-xs leading-relaxed\">\n              Any authenticator app works. The code refreshes every 30 seconds and never leaves\n              your phone.\n            </p>\n          </div>\n\n          <div className=\"px-6 py-10 md:col-span-7 md:px-12 md:py-14\">\n            {status === \"success\" ? (\n              <div className=\"max-w-md\">\n                <p className=\"text-muted-foreground font-mono text-xs tracking-wider uppercase\">\n                  Verified\n                </p>\n                <h2\n                  id={titleId}\n                  ref={successHeadingRef}\n                  tabIndex={-1}\n                  className=\"mt-4 text-4xl font-semibold tracking-[-0.03em] outline-none md:text-5xl\"\n                >\n                  Signed in\n                </h2>\n                <p className=\"text-muted-foreground mt-5 text-base leading-relaxed\">\n                  {trustDevice\n                    ? `We won’t ask for a code on this browser for ${trustDays} days. You can revoke it in Settings → Security.`\n                    : \"We’ll ask for a code again next time you sign in on this browser.\"}\n                </p>\n                <Button\n                  asChild\n                  size=\"lg\"\n                  className=\"mt-10 rounded-none bg-[var(--block-accent)] text-[var(--block-accent-fg)] shadow-none hover:bg-[var(--block-accent)] hover:underline hover:underline-offset-4\"\n                >\n                  <a href={continueHref}>Continue to {brand}</a>\n                </Button>\n              </div>\n            ) : (\n              <div className=\"max-w-md\">\n                <p className=\"text-muted-foreground font-mono text-xs tracking-wider uppercase\">\n                  Signing in as <span className=\"text-foreground normal-case\">{account}</span>\n                </p>\n                <h2\n                  id={titleId}\n                  className=\"mt-4 text-4xl font-semibold tracking-[-0.03em] text-balance md:text-5xl\"\n                >\n                  {title}\n                </h2>\n                <p id={hintId} className=\"text-muted-foreground mt-5 text-base leading-relaxed\">\n                  {method === \"authenticator\"\n                    ? description\n                    : \"Enter one of the 8-character backup codes you saved when you turned on two-step verification. Each code works once.\"}\n                </p>\n\n                <form\n                  noValidate\n                  onSubmit={handleSubmit}\n                  aria-busy={status === \"submitting\"}\n                  className=\"mt-10 grid gap-6\"\n                >\n                  {method === \"authenticator\" ? (\n                    <div className=\"grid gap-3\">\n                      <Label htmlFor={codeId}>Authentication code</Label>\n                      <InputOTP\n                        ref={codeRef}\n                        id={codeId}\n                        name=\"code\"\n                        maxLength={CODE_LENGTH}\n                        pattern={REGEXP_ONLY_DIGITS}\n                        inputMode=\"numeric\"\n                        autoComplete=\"one-time-code\"\n                        required\n                        value={code}\n                        onChange={(value) => {\n                          setCode(value);\n                          if (error) setError(undefined);\n                        }}\n                        readOnly={status === \"submitting\"}\n                        aria-invalid={error ? true : undefined}\n                        aria-describedby={describedBy}\n                        containerClassName=\"gap-3\"\n                      >\n                        <InputOTPGroup>\n                          {[0, 1, 2].map((index) => (\n                            <InputOTPSlot\n                              key={index}\n                              index={index}\n                              aria-invalid={error ? true : undefined}\n                              className=\"h-12 w-11 font-mono text-lg\"\n                            />\n                          ))}\n                        </InputOTPGroup>\n                        <InputOTPSeparator className=\"text-muted-foreground\" />\n                        <InputOTPGroup>\n                          {[3, 4, 5].map((index) => (\n                            <InputOTPSlot\n                              key={index}\n                              index={index}\n                              aria-invalid={error ? true : undefined}\n                              className=\"h-12 w-11 font-mono text-lg\"\n                            />\n                          ))}\n                        </InputOTPGroup>\n                      </InputOTP>\n                    </div>\n                  ) : (\n                    <div className=\"grid gap-2\">\n                      <Label htmlFor={backupId}>Backup code</Label>\n                      <Input\n                        ref={backupRef}\n                        id={backupId}\n                        name=\"backup-code\"\n                        type=\"text\"\n                        autoComplete=\"one-time-code\"\n                        autoCapitalize=\"characters\"\n                        autoCorrect=\"off\"\n                        spellCheck={false}\n                        maxLength={9}\n                        required\n                        placeholder=\"XXXX-XXXX\"\n                        value={backup}\n                        onChange={(event) => setBackup(event.target.value)}\n                        onBlur={() => {\n                          if (submitted) setError(validate(\"backup\", code, backup));\n                        }}\n                        readOnly={status === \"submitting\"}\n                        aria-invalid={error ? true : undefined}\n                        aria-describedby={describedBy}\n                        className=\"max-w-60 font-mono tracking-widest uppercase\"\n                      />\n                    </div>\n                  )}\n\n                  {error ? (\n                    <p id={errorId} className=\"text-destructive -mt-3 text-sm\">\n                      {error}\n                    </p>\n                  ) : null}\n\n                  <div className=\"flex items-start gap-3\">\n                    <Checkbox\n                      id={trustId}\n                      name=\"trust-device\"\n                      checked={trustDevice}\n                      onCheckedChange={(checked) => setTrustDevice(checked === true)}\n                      className=\"mt-0.5\"\n                    />\n                    <div className=\"grid gap-1\">\n                      <Label htmlFor={trustId} className=\"font-normal\">\n                        Trust this device for {trustDays} days\n                      </Label>\n                      <p className=\"text-muted-foreground text-xs\">\n                        Only on a computer you don’t share.\n                      </p>\n                    </div>\n                  </div>\n\n                  <Button\n                    type=\"submit\"\n                    size=\"lg\"\n                    disabled={status === \"submitting\"}\n                    className=\"w-full rounded-none bg-[var(--block-accent)] text-[var(--block-accent-fg)] shadow-none hover:bg-[var(--block-accent)] hover:underline hover:underline-offset-4 sm:w-auto sm:justify-self-start\"\n                  >\n                    {status === \"submitting\" ? (\n                      <>\n                        <Loader2 aria-hidden=\"true\" className=\"animate-spin motion-reduce:animate-none\" />\n                        Verifying…\n                      </>\n                    ) : (\n                      \"Verify and sign in\"\n                    )}\n                  </Button>\n                </form>\n\n                <ul className=\"mt-10 divide-y border-y text-sm\">\n                  <li>\n                    <button\n                      type=\"button\"\n                      onClick={switchMethod}\n                      className=\"flex w-full items-center justify-between gap-4 rounded-sm py-3 text-left font-medium outline-none hover:underline hover:underline-offset-4 focus-visible:ring-2 focus-visible:ring-ring\"\n                    >\n                      {method === \"authenticator\"\n                        ? \"Use a backup code instead\"\n                        : \"Use your authenticator app instead\"}\n                      <span aria-hidden=\"true\" className=\"text-muted-foreground font-mono text-xs\">\n                        →\n                      </span>\n                    </button>\n                  </li>\n                  <li>\n                    <a\n                      href={supportHref}\n                      className=\"flex w-full items-center justify-between gap-4 rounded-sm py-3 outline-none hover:underline hover:underline-offset-4 focus-visible:ring-2 focus-visible:ring-ring\"\n                    >\n                      Lost access? Contact support\n                      <span aria-hidden=\"true\" className=\"text-muted-foreground font-mono text-xs\">\n                        Replies in 1 working day\n                      </span>\n                    </a>\n                  </li>\n                </ul>\n\n                <p className=\"text-muted-foreground mt-6 text-xs\">\n                  Not you?{\" \"}\n                  <a\n                    href={signInHref}\n                    className=\"text-foreground rounded-sm underline-offset-4 outline-none hover:underline focus-visible:ring-2 focus-visible:ring-ring\"\n                  >\n                    Sign in with a different account\n                  </a>\n                </p>\n              </div>\n            )}\n\n            <p role=\"status\" aria-live=\"polite\" className=\"sr-only\">\n              {announcement}\n            </p>\n          </div>\n        </div>\n      </div>\n    </section>\n  );\n}\n"
    }
  ],
  "dependencies": [
    "input-otp",
    "lucide-react"
  ],
  "registryDependencies": [
    "button",
    "checkbox",
    "input",
    "input-otp",
    "label"
  ]
}
