# Passkey sign-in (通行密钥登录)

Sign-in card with a passkey button and an email-link fallback: verifying, first-visit, failed-verification and link-sent states.

- Ref: `component/passkey-sign-in` (component · auth)
- Page: https://uifino.com/en/components/auth/passkey-sign-in
- Markdown: https://uifino.com/md/components/passkey-sign-in.md
- Preview image: https://uifino.com/blocks/covers/passkey-sign-in-dark.webp

## Install

- shadcn: `npx shadcn@latest add "https://uifino.com/r/blocks/passkey-sign-in.json"`
- UIFino CLI: `npx uifino@latest add component/passkey-sign-in`

## Dependencies

`lucide-react`

## shadcn/ui components it builds on

- `button`, `card`, `field`, `input`, `spinner`. The shadcn CLI installs them together with the block.

## Theme

The block reads the project's shadcn theme variables, so it follows whatever DESIGN.md the project uses and needs no options of its own.
To give the project the look of a UIFino brand or style, install its theme: `npx shadcn@latest add "https://uifino.com/r/themes/<brand-or-style-id>.json"`.

## Usage

The files under `examples/` show the block wired with content, including its loading, empty and error states where it has them.

## Files

| Path | Role | Size |
| :--- | :--- | ---: |
| components/blocks/passkey-sign-in/examples/content.ts | demo | 2.5 KB |
| components/blocks/passkey-sign-in/examples/default.tsx | demo | 517 B |
| components/blocks/passkey-sign-in/examples/empty.tsx | demo | 514 B |
| components/blocks/passkey-sign-in/examples/error.tsx | demo | 540 B |
| components/blocks/passkey-sign-in/examples/loading.tsx | demo | 544 B |
| components/blocks/passkey-sign-in/examples/sent.tsx | demo | 539 B |
| components/blocks/passkey-sign-in/passkey-sign-in.tsx | source | 6.0 KB |

### components/blocks/passkey-sign-in/passkey-sign-in.tsx

```tsx
"use client";

import { type FormEvent, useId, useState } from "react";
import { ArrowLeft, CircleAlert, Fingerprint, MailCheck, type LucideIcon } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
import { Field, FieldError, FieldGroup, FieldLabel, FieldSeparator } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { Spinner } from "@/components/ui/spinner";
import { cn } from "@/lib/utils";

export type PasskeySignInStatus = "ready" | "verifying" | "no-passkey" | "error" | "sent";

export interface PasskeySignInLabels {
  readonly passkey: string;
  readonly verifying: string;
  readonly emptyDescription: string;
  readonly divider: string;
  readonly emailLabel: string;
  readonly emailPlaceholder: string;
  readonly emailInvalid: string;
  readonly sendLink: string;
  readonly continueWithEmail: string;
  readonly errorTitle: string;
  readonly errorDescription: string;
  readonly retry: string;
  readonly sentTitle: string;
  readonly sentDescription: string;
  readonly changeEmail: string;
  readonly footer: string;
}

export interface PasskeySignInProps {
  readonly icon: LucideIcon;
  readonly brand: string;
  readonly title: string;
  readonly description: string;
  readonly labels: PasskeySignInLabels;
  readonly status?: PasskeySignInStatus;
  readonly defaultEmail?: string;
  readonly onPasskey?: () => void;
  readonly onEmailSubmit?: (email: string) => void;
  readonly onReset?: () => void;
  readonly className?: string;
}

const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

export function PasskeySignIn({
  icon: Icon,
  brand,
  title,
  description,
  labels,
  status = "ready",
  defaultEmail = "",
  onPasskey,
  onEmailSubmit,
  onReset,
  className
}: PasskeySignInProps) {
  const emailId = useId();
  const [email, setEmail] = useState(defaultEmail);
  const [invalid, setInvalid] = useState(false);

  const verifying = status === "verifying";
  const hasPasskey = status !== "no-passkey";

  const submit = (event: FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    const value = email.trim();
    if (!EMAIL_PATTERN.test(value)) {
      setInvalid(true);
      return;
    }
    setInvalid(false);
    onEmailSubmit?.(value);
  };

  if (status === "sent") {
    return (
      <Card className={cn("@container w-full max-w-sm", className)}>
        <CardHeader>
          <CardTitle className="flex items-center gap-2 text-xl">
            <MailCheck className="size-5 shrink-0" aria-hidden="true" />
            {labels.sentTitle}
          </CardTitle>
          <CardDescription>{labels.sentDescription}</CardDescription>
        </CardHeader>
        <CardContent>
          <p className="truncate rounded-md border bg-muted/40 px-3 py-2 text-sm font-medium">{email}</p>
        </CardContent>
        <CardFooter>
          <Button variant="ghost" size="sm" onClick={onReset}>
            <ArrowLeft />
            {labels.changeEmail}
          </Button>
        </CardFooter>
      </Card>
    );
  }

  return (
    <Card className={cn("@container w-full max-w-sm", className)}>
      <CardHeader>
        <div className="flex items-center gap-2 text-sm font-medium">
          <Icon className="size-4 shrink-0" aria-hidden="true" />
          <span className="truncate">{brand}</span>
        </div>
        <CardTitle className="truncate text-xl">{title}</CardTitle>
        <CardDescription className="text-pretty">{hasPasskey ? description : labels.emptyDescription}</CardDescription>
      </CardHeader>
      <CardContent>
        <FieldGroup className="gap-5">
          {status === "error" && (
            <div role="alert" className="flex gap-3 text-sm">
              <CircleAlert className="mt-0.5 size-4 shrink-0 text-destructive" aria-hidden="true" />
              <div className="flex min-w-0 flex-col gap-1">
                <p className="font-medium text-destructive">{labels.errorTitle}</p>
                <p className="text-pretty text-muted-foreground">{labels.errorDescription}</p>
              </div>
            </div>
          )}

          {hasPasskey && (
            <Button
              size="lg"
              className="w-full"
              disabled={verifying}
              aria-busy={verifying}
              onClick={onPasskey}
            >
              {verifying ? <Spinner /> : <Fingerprint />}
              <span className="truncate">
                {verifying ? labels.verifying : status === "error" ? labels.retry : labels.passkey}
              </span>
            </Button>
          )}

          {hasPasskey && (
            <FieldSeparator className="*:data-[slot=field-separator-content]:bg-card">{labels.divider}</FieldSeparator>
          )}

          <form onSubmit={submit} noValidate className="flex flex-col gap-3">
            <Field data-invalid={invalid || undefined}>
              <FieldLabel htmlFor={emailId}>{labels.emailLabel}</FieldLabel>
              <Input
                id={emailId}
                type="email"
                autoComplete="username webauthn"
                placeholder={labels.emailPlaceholder}
                value={email}
                aria-invalid={invalid}
                onChange={(event) => {
                  setEmail(event.target.value);
                  if (invalid) setInvalid(false);
                }}
              />
              {invalid && <FieldError>{labels.emailInvalid}</FieldError>}
            </Field>
            <Button
              type="submit"
              variant={hasPasskey ? "outline" : "default"}
              size="lg"
              className="w-full"
              disabled={verifying}
            >
              {hasPasskey ? labels.sendLink : labels.continueWithEmail}
            </Button>
          </form>
        </FieldGroup>
      </CardContent>
      <CardFooter className="justify-center">
        <p className="text-balance text-center text-sm text-muted-foreground">{labels.footer}</p>
      </CardFooter>
    </Card>
  );
}
```

Not inlined (6 files, 5.1 KB). Full sources: https://uifino.com/r/blocks/passkey-sign-in.json
