# Magic link

> Passwordless sign-in card with OAuth buttons, email typo suggestion and check-your-inbox state with Gmail/Outlook shortcuts.

- Kind: block · Category: Auth · Interactivity: script
- Install: `npx shadcn@latest add @haze/magic-link`
- File: `src/components/haze/blocks/magic-link.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/blocks/magic-link

## Use when
- Offering passwordless email sign-in with one-click typo correction and inbox shortcuts
- Transitioning cleanly from email entry to a resend-ready check-your-inbox state

## Avoid when
- Requiring a 6-digit typed verification code instead of a link; use auth-login or otp-verification

## Alternatives
- [auth-login](https://hazeui.com/blocks/auth-login.md)
- [auth-sign-in-card](https://hazeui.com/blocks/auth-sign-in-card.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` | "haze-magic-link" | no | Unique prefix for email label and heading IDs. |
| heading | `string` | "Sign in without a password" | no | Card heading. |
| class | `string` |  | no | Extra classes on the outer section. |

## Accessibility
- The email typo suggestion uses an accessible inline button that updates the input directly and announces the fix.
- Sending, resending and switching back announce status via a polite role="status" message.
- Key Tab: Move across OAuth buttons, email input, typo fix button and inbox actions
- Key Enter / Space: Accept the typo fix, send the magic link or resend it

## Agent prompts

### Install and use

Install the Haze UI block "Magic link" with `npx shadcn@latest add @haze/magic-link`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import MagicLink from "@/components/haze/blocks/magic-link.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/blocks/magic-link.md

### Customize with real content

Adapt the Haze magic-link block for our product. Replace the demo values of these props with our real content: id, heading, class. Keep the layout, the heading level rules and these accessibility details: The email typo suggestion uses an accessible inline button that updates the input directly and announces the fix. Sending, resending and switching back announce status via a polite role="status" message. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using magic-link, check it fits: use it when offering passwordless email sign-in with one-click typo correction and inbox shortcuts; transitioning cleanly from email entry to a resend-ready check-your-inbox state. Avoid it when requiring a 6-digit typed verification code instead of a link; use auth-login or otp-verification. Alternatives: @haze/auth-login, @haze/auth-sign-in-card.

## Example requests that should lead here
- Add a passwordless magic link auth card with OAuth, email typo suggestion and a check-your-inbox screen

## Source

```astro
---
import { ArrowRight, ExternalLink, Mail, RotateCw, Sparkles } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";

interface Props {
  id?: string;
  heading?: string;
  class?: string;
}

const { id = "haze-magic-link", heading = "Sign in without a password", class: className } = Astro.props;
---

<section aria-labelledby={`${id}-title`} data-haze-magic-link class={cn("bg-background px-4 py-10 sm:px-6 sm:py-14", className)}>
  <div class="mx-auto max-w-md rounded-2xl border border-border bg-card p-6 shadow-float sm:p-8">
    <div data-ml-step="form">
      <span class="inline-flex items-center gap-1.5 rounded-full border border-frame bg-muted px-2.5 py-0.5 text-xs font-medium text-foreground">
        <Sparkles class="size-3.5" aria-hidden="true" />
        Passwordless
      </span>
      <h2 id={`${id}-title`} class="mt-3 text-xl/7 font-semibold tracking-tight text-foreground">{heading}</h2>
      <p class="mt-1 text-xs/5 text-muted-foreground">We will email a one-time magic sign-in link that expires in 10 minutes.</p>

      <div class="mt-5 grid grid-cols-2 gap-2">
        <button type="button" data-ml-oauth="Google" class="inline-flex h-9 cursor-pointer items-center justify-center rounded-lg border border-frame bg-field text-xs font-medium text-foreground hover:border-frame-strong hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">Continue with Google</button>
        <button type="button" data-ml-oauth="GitHub" class="inline-flex h-9 cursor-pointer items-center justify-center rounded-lg border border-frame bg-field text-xs font-medium text-foreground hover:border-frame-strong hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">Continue with GitHub</button>
      </div>

      <form data-ml-form class="mt-5 grid gap-3">
        <div class="grid gap-1.5">
          <label for={`${id}-email`} class="text-xs font-medium text-foreground">Work email</label>
          <input id={`${id}-email`} data-ml-email type="email" required value="nina@gmial.com" class="h-10 rounded-lg border border-frame-input bg-field px-3 text-sm text-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/24 focus-visible:outline-hidden transition-[color,background-color,border-color,box-shadow] duration-150 hover:border-frame-foreground" />
          <p data-ml-typo class="text-xs text-warning">
            Did you mean <button type="button" data-ml-fix="nina@gmail.com" class="cursor-pointer font-medium underline underline-offset-4 hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring">nina@gmail.com</button>?
          </p>
        </div>
        <button type="submit" class="inline-flex h-10 cursor-pointer items-center justify-center gap-1.5 rounded-lg bg-primary text-sm font-medium text-primary-foreground hover:opacity-90 focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
          Send magic link
          <ArrowRight class="size-4" aria-hidden="true" />
        </button>
      </form>
    </div>

    <div data-ml-step="sent" hidden class="space-y-4 text-center">
      <span class="mx-auto grid size-12 place-items-center rounded-2xl border border-frame bg-muted text-muted-foreground" aria-hidden="true">
        <Mail class="size-5" />
      </span>
      <h3 class="text-lg/7 font-semibold text-foreground">Check your inbox</h3>
      <p class="text-xs/5 text-pretty text-muted-foreground">
        We sent a one-time sign-in link to <strong data-ml-dest class="text-foreground">nina@gmail.com</strong>.
      </p>
      <div class="grid grid-cols-2 gap-2 pt-1">
        <a href="#open-gmail" class="inline-flex h-9 items-center justify-center gap-1.5 rounded-lg border border-frame bg-muted/40 text-xs font-medium text-foreground hover:border-frame-strong hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring">
          Open Gmail
          <ExternalLink class="size-3.5" aria-hidden="true" />
        </a>
        <a href="#open-outlook" class="inline-flex h-9 items-center justify-center gap-1.5 rounded-lg border border-frame bg-muted/40 text-xs font-medium text-foreground hover:border-frame-strong hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring">
          Open Outlook
          <ExternalLink class="size-3.5" aria-hidden="true" />
        </a>
      </div>
      <div class="flex items-center justify-center gap-3 pt-1 text-xs">
        <button type="button" data-ml-resend class="inline-flex h-8 cursor-pointer items-center gap-1 rounded-md px-2 font-medium text-foreground hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
          <RotateCw class="size-3" aria-hidden="true" />
          Resend link
        </button>
        <button type="button" data-ml-back class="inline-flex h-8 cursor-pointer items-center rounded-md px-2 text-muted-foreground hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">Use another email</button>
      </div>
    </div>

    <p data-ml-status role="status" class="mt-4 min-h-4 text-xs/4 text-muted-foreground"></p>
  </div>
</section>

<script>
  function initMagicLinks() {
    document.querySelectorAll<HTMLElement>("[data-haze-magic-link]").forEach((root) => {
      if (root.hasAttribute("data-enhanced")) return;
      root.setAttribute("data-enhanced", "");
      const email = root.querySelector<HTMLInputElement>("[data-ml-email]");
      const typo = root.querySelector<HTMLElement>("[data-ml-typo]");
      const stepForm = root.querySelector<HTMLElement>('[data-ml-step="form"]');
      const stepSent = root.querySelector<HTMLElement>('[data-ml-step="sent"]');
      const dest = root.querySelector<HTMLElement>("[data-ml-dest]");
      const status = root.querySelector<HTMLElement>("[data-ml-status]");

      root.querySelector<HTMLButtonElement>("[data-ml-fix]")?.addEventListener("click", (ev) => {
        const fixed = (ev.currentTarget as HTMLButtonElement).dataset.mlFix ?? "";
        if (email) email.value = fixed;
        if (typo) typo.hidden = true;
        if (status) status.textContent = `Updated email to ${fixed}.`;
      });

      email?.addEventListener("input", () => {
        if (typo) typo.hidden = !/gmial\.com/i.test(email.value);
      });

      root.querySelector<HTMLFormElement>("[data-ml-form]")?.addEventListener("submit", (ev) => {
        ev.preventDefault();
        if (dest && email) dest.textContent = email.value;
        if (stepForm) stepForm.hidden = true;
        if (stepSent) stepSent.hidden = false;
        if (status) status.textContent = `Magic link sent to ${email?.value ?? "your inbox"}.`;
      });

      root.querySelector<HTMLButtonElement>("[data-ml-resend]")?.addEventListener("click", () => {
        if (status) status.textContent = "Sent a new magic link.";
      });
      root.querySelector<HTMLButtonElement>("[data-ml-back]")?.addEventListener("click", () => {
        if (stepForm) stepForm.hidden = false;
        if (stepSent) stepSent.hidden = true;
      });
      root.querySelectorAll<HTMLButtonElement>("[data-ml-oauth]").forEach((btn) => {
        btn.addEventListener("click", () => {
          if (status) status.textContent = `Demo OAuth sign-in with ${btn.dataset.mlOauth}.`;
        });
      });
    });
  }
  initMagicLinks();
  document.addEventListener("astro:page-load", initMagicLinks);
</script>
```
