Two-Factor Setup Prompt
Security prompt inviting two-factor setup, with an inline toggle once enabled.
"use client"
import { useState } from "react"
import { LockKeyIcon, ShieldCheckIcon, XIcon } from "@phosphor-icons/react"
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
import { Button } from "@/components/ui/button"
import { Switch } from "@/components/ui/switch"
const COPY = {
title: "Add a second step to sign-in",
body: "Two-factor authentication keeps your account safe even if your password leaks.",
enable: "Turn on two-factor",
later: "Not now",
dismiss: "Dismiss security prompt",
onTitle: "Two-factor authentication is on",
onBody:
"You will be asked for a code from your authenticator app at sign-in.",
turnOff: "Turn off",
hidden: "We will ask again next week.",
show: "Show",
} as const
const BENEFITS = [
"Works with any authenticator app",
"Takes about two minutes to set up",
"Backup codes if you lose your phone",
] as const
/** Security prompt inviting two-factor setup, with an inline toggle once enabled. */
export default function AlertTwoFactorPrompt01() {
const [on, setOn] = useState(false)
const [open, setOpen] = useState(true)
if (on) {
return (
<Alert className="mx-auto max-w-xl">
<ShieldCheckIcon aria-hidden="true" />
<AlertTitle>{COPY.onTitle}</AlertTitle>
<AlertDescription>{COPY.onBody}</AlertDescription>
<div className="col-start-2 mt-3 flex items-center gap-2 text-sm">
<Switch
checked={on}
onCheckedChange={setOn}
aria-label={COPY.turnOff}
/>
<span className="text-muted-foreground">{COPY.turnOff}</span>
</div>
</Alert>
)
}
if (!open) {
return (
<p className="mx-auto flex w-full max-w-xl items-center justify-between rounded-2xl border border-dashed px-4 py-3 text-sm text-muted-foreground">
{COPY.hidden}
<Button size="xs" variant="ghost" onClick={() => setOpen(true)}>
{COPY.show}
</Button>
</p>
)
}
return (
<Alert className="mx-auto max-w-xl pr-12">
<LockKeyIcon aria-hidden="true" />
<AlertTitle>{COPY.title}</AlertTitle>
<AlertDescription>{COPY.body}</AlertDescription>
<ul className="col-start-2 mt-3 list-disc pl-5 text-sm text-muted-foreground">
{BENEFITS.map((b) => (
<li key={b}>{b}</li>
))}
</ul>
<div className="col-start-2 mt-4 flex gap-2">
<Button size="sm" onClick={() => setOn(true)}>
{COPY.enable}
</Button>
<Button size="sm" variant="outline" onClick={() => setOpen(false)}>
{COPY.later}
</Button>
</div>
<Button
size="icon-xs"
variant="ghost"
className="absolute top-2.5 right-3"
aria-label={COPY.dismiss}
onClick={() => setOpen(false)}
>
<XIcon />
</Button>
</Alert>
)
}
Installation
pnpm dlx shadcn@latest add @01orbital/two-factor-prompt-01npx shadcn@latest add @01orbital/two-factor-prompt-01yarn dlx shadcn@latest add @01orbital/two-factor-prompt-01bunx --bun shadcn@latest add @01orbital/two-factor-prompt-01Make it yours
- Its words are in
COPYat the top ofcomponents/blocks/alert/two-factor-prompt-01/index.tsx: change them there, not in the markup. BENEFITS, also at the top of the file, holds its lists and settings.
Built from
npm: @phosphor-icons/react. The CLI installs it for you.
Built from these parts. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Last updated on