# Two Factor Verification

> A hero with a two factor verification form for a one time security code. Confirms identity during signin to keep new accounts safe and secure.

A shadcn/ui block from the [@shadcn-ui-blocks](https://www.shadcn-ui-blocks.com) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/shadcn-ui-blocks/marketing-hero-forms-two-factor-verification).

- Page, with a live preview: https://shadcn.exchange/r/shadcn-ui-blocks/marketing-hero-forms-two-factor-verification
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/shadcn-ui-blocks/marketing-hero-forms-two-factor-verification-light-77500101b99e0c74.png
- Registry item: https://www.shadcn-ui-blocks.com/r/marketing-hero-forms-two-factor-verification.json
- Listed: 2026-10-13, source last changed 2026-10-13

## Install

```bash
npx shadcn@latest add https://www.shadcn-ui-blocks.com/r/marketing-hero-forms-two-factor-verification.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- npm packages: `class-variance-authority`, `lucide-react`, `radix-ui`

## Files

### `components/blocks/marketing/hero-forms/two-factor-verification.tsx`

```tsx
import { useState, useRef, useEffect } from 'react';

import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import {
  ArrowLeftIcon,
  LockIcon,
  KeyIcon,
  SmartphoneIcon,
  MailIcon,
  CheckCircleIcon,
} from 'lucide-react';

export default function HeroFormTwoFactorVerification() {
  const [step, setStep] = useState(1);
  const [verificationCode, setVerificationCode] = useState([
    '',
    '',
    '',
    '',
    '',
    '',
  ]);
  const [isVerified, setIsVerified] = useState(false);
  const inputRefs = useRef<(HTMLInputElement | null)[]>([]);

  // Initialize refs array
  useEffect(() => {
    inputRefs.current = inputRefs.current.slice(0, 6);
  }, []);

  const handleCodeChange = (index: number, value: string) => {
    if (value.length > 1) {
      value = value.charAt(0);
    }

    // Update verification code
    const newCode = [...verificationCode];
    newCode[index] = value;
    setVerificationCode(newCode);

    // Auto-focus next input
    if (value && index < 5) {
      inputRefs.current[index + 1]?.focus();
    }

    // Check if code is complete
    if (newCode.every((digit) => digit !== '') && index === 5) {
      // In a real app, you'd validate the code here
      setTimeout(() => {
        setIsVerified(true);
      }, 1000);
    }
  };

  const handleKeyDown = (
    index: number,
    e: React.KeyboardEvent<HTMLInputElement>
  ) => {
    // Move to previous input on backspace if current input is empty
    if (e.key === 'Backspace' && index > 0 && !verificationCode[index]) {
      inputRefs.current[index - 1]?.focus();
    }
  };

  return (
    <>
      {/* Hero */}
      <div className="relative min-h-screen overflow-hidden">
        <div className="container mx-auto flex min-h-screen items-center justify-center px-4 py-16 md:px-6 2xl:max-w-[1400px]">
          <div className="w-full max-w-md">
            <div className="mb-8 text-center">
              <div className="bg-primary/10 mx-auto mb-6 flex h-16 w-16 items-center justify-center rounded-full">
                <LockIcon className="text-primary h-8 w-8" />
              </div>

              {step === 1 && (
                <>
                  <h1 className="mb-2 text-3xl font-bold">
                    Two-factor authentication
                  </h1>
                  <p className="text-muted-foreground">
                    Choose a verification method to secure your account
                  </p>
                </>
              )}

              {step === 2 && !isVerified && (
                <>
                  <h1 className="mb-2 text-3xl font-bold">
                    Enter verification code
                  </h1>
                  <p className="text-muted-foreground">
                    We&apos;ve sent a 6-digit code to your email address or
                    phone
                  </p>
                </>
              )}

              {isVerified && (
                <>
                  <h1 className="mb-2 text-3xl font-bold">
                    Verification successful
                  </h1>
                  <p className="text-muted-foreground">
                    Your account is now secured with two-factor authentication
                  </p>
                </>
              )}
            </div>

            <div className="bg-background rounded-lg border p-8 shadow-lg">
              {/* Step 1: Choose verification method */}
              {step === 1 && (
                <div className="space-y-4">
                  <p className="mb-6 text-sm">
                    Add an extra layer of security to your account by enabling
                    two-factor authentication.
                  </p>

                  <div className="space-y-3">
                    <button
                      onClick={() => setStep(2)}
                      className="hover:bg-muted/50 flex w-full items-center gap-3 rounded-lg border p-4 text-left transition-colors"
                    >
                      <div className="bg-primary/10 rounded-full p-2">
                        <SmartphoneIcon className="text-primary h-5 w-5" />
                      </div>
                      <div>
                        <h3 className="font-medium">Text message (SMS)</h3>
                        <p className="text-muted-foreground text-sm">
                          We&apos;ll send a code to your phone
                        </p>
                      </div>
                    </button>

                    <button
                      onClick={() => setStep(2)}
                      className="hover:bg-muted/50 flex w-full items-center gap-3 rounded-lg border p-4 text-left transition-colors"
                    >
                      <div className="bg-primary/10 rounded-full p-2">
                        <MailIcon className="text-primary h-5 w-5" />
                      </div>
                      <div>
                        <h3 className="font-medium">Email</h3>
                        <p className="text-muted-foreground text-sm">
                          We&apos;ll email you a verification code
                        </p>
                      </div>
                    </button>

                    <button
                      onClick={() => setStep(2)}
                      className="hover:bg-muted/50 flex w-full items-center gap-3 rounded-lg border p-4 text-left transition-colors"
                    >
                      <div className="bg-primary/10 rounded-full p-2">
                        <KeyIcon className="text-primary h-5 w-5" />
                      </div>
                      <div>
                        <h3 className="font-medium">Authentication app</h3>
                        <p className="text-muted-foreground text-sm">
                          Use an authentication app like Google Authenticator
                        </p>
                      </div>
                    </button>
                  </div>
                </div>
              )}

              {/* Step 2: Enter verification code */}
              {step === 2 && !isVerified && (
                <div className="space-y-6">
                  <div className="mb-2 flex justify-center">
                    <div className="flex gap-2">
                      {[0, 1, 2, 3, 4, 5].map((index) => (
                        <Input
                          key={index}
                          type="text"
                          maxLength={1}
                          value={verificationCode[index]}
                          onChange={(e) =>
                            handleCodeChange(index, e.target.value)
                          }
                          onKeyDown={(e) => handleKeyDown(index, e)}
                          ref={(el) => {
                            inputRefs.current[index] = el;
                          }}
                          className="h-14 w-12 text-center text-xl font-semibold"
                        />
                      ))}
                    </div>
                  </div>

                  <div className="text-center text-sm">
                    <p className="text-muted-foreground mb-2">
                      Didn&apos;t receive a code?
                    </p>
                    <Button variant="link" className="h-auto p-0">
                      Resend code
                    </Button>
                  </div>

                  <div className="pt-4">
                    <Button
                      variant="outline"
                      onClick={() => setStep(1)}
                      className="mb-3 w-full"
                    >
                      <ArrowLeftIcon className="mr-2 h-4 w-4" />
                      Back
                    </Button>
                    <Button
                      className="w-full"
                      disabled={
                        !verificationCode.every((digit) => digit !== '')
                      }
                    >
                      Verify
                    </Button>
                  </div>
                </div>
              )}

              {/* Success state */}
              {isVerified && (
                <div className="space-y-6 text-center">
                  <div className="mx-auto flex h-20 w-20 items-center justify-center rounded-full bg-green-100">
                    <CheckCircleIcon className="h-10 w-10 text-green-600" />
                  </div>

                  <p className="text-sm">
                    You can now sign in using your password and the verification
                    code.
                  </p>

                  <Button className="w-full">Continue to Dashboard</Button>
                </div>
              )}
            </div>

            <p className="text-muted-foreground mt-8 text-center text-sm">
              Need help?{' '}
              <a href="#" className="text-primary hover:underline">
                Contact support
              </a>
            </p>
          </div>
        </div>
      </div>
      {/* End Hero */}
    </>
  );
}
```

### `components/ui/button.tsx`

```tsx
import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { Slot } from "radix-ui"

import { cn } from "@/lib/utils"

const buttonVariants = cva(
  "group/button inline-flex shrink-0 items-center justify-center rounded-md border border-transparent bg-clip-padding text-sm font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:not-aria-[haspopup]:translate-y-px disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
  {
    variants: {
      variant: {
        default: "bg-primary text-primary-foreground hover:bg-primary/80",
        outline:
          "border-border bg-background shadow-xs hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:bg-input/50",
        secondary:
          "bg-secondary text-secondary-foreground hover:bg-secondary/80 aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",
        ghost:
          "hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:bg-muted/50",
        destructive:
          "bg-destructive/10 text-destructive hover:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30 dark:focus-visible:ring-destructive/40",
        link: "text-primary underline-offset-4 hover:underline",
      },
      size: {
        default:
          "h-9 gap-1.5 px-2.5 in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
        xs: "h-6 gap-1 rounded-[min(var(--radius-md),8px)] px-2 text-xs in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3",
        sm: "h-8 gap-1 rounded-[min(var(--radius-md),10px)] px-2.5 in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5",
        lg: "h-10 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
        icon: "size-9",
        "icon-xs":
          "size-6 rounded-[min(var(--radius-md),8px)] in-data-[slot=button-group]:rounded-md [&_svg:not([class*='size-'])]:size-3",
        "icon-sm":
          "size-8 rounded-[min(var(--radius-md),10px)] in-data-[slot=button-group]:rounded-md",
        "icon-lg": "size-10",
      },
    },
    defaultVariants: {
      variant: "default",
      size: "default",
    },
  }
)

function Button({
  className,
  variant = "default",
  size = "default",
  asChild = false,
  ...props
}: React.ComponentProps<"button"> &
  VariantProps<typeof buttonVariants> & {
    asChild?: boolean
  }) {
  const Comp = asChild ? Slot.Root : "button"

  return (
    <Comp
      data-slot="button"
      data-variant={variant}
      data-size={size}
      className={cn(buttonVariants({ variant, size, className }))}
      {...props}
    />
  )
}

export { Button, buttonVariants }
```

### `components/ui/input.tsx`

```tsx
import * as React from "react"

import { cn } from "@/lib/utils"

function Input({ className, type, ...props }: React.ComponentProps<"input">) {
  return (
    <input
      type={type}
      data-slot="input"
      className={cn(
        "h-9 w-full min-w-0 rounded-md border border-input bg-transparent px-2.5 py-1 text-base shadow-xs transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",
        className
      )}
      {...props}
    />
  )
}

export { Input }
```
