Instant's magic code auth behind an animated code card: the user types an email, Instant sends a 6-digit code, and the card checks it with signInWithMagicCode and opens the app.
"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { StepUpGate } from "@/components/StepUpGate";
import { db } from "@/lib/db";
type InstantError = { body?: { message?: string } };
export default function LoginPage() {
const router = useRouter();
const [email, setEmail] = useState("");
const [codeSent, setCodeSent] = useState(false);
const [error, setError] = useState("");
async function sendCode() {
setError("");
try {
await db.auth.sendMagicCode({ email });
setCodeSent(true);
} catch (err) {
setError((err as InstantError).body?.message ?? "Couldn't send the code. Try again.");
}
}
if (codeSent) {
return (
<StepUpGate
title="Check your email"
description={`Enter the 6-digit code we sent to ${email}.`}
successTitle="You're in"
successDescription="Opening your app."
onVerify={async (code) => {
try {
await db.auth.signInWithMagicCode({ email, code });
return true;
} catch {
return false; // wrong or expired code: the cells shake and the user types again
}
}}
onResend={() => void db.auth.sendMagicCode({ email })}
onCancel={() => setCodeSent(false)}
onVerified={() => router.push("/app")}
>
{/* whatever should sit blurred behind the code card */}
<div className="grid min-h-dvh place-items-center bg-[#0b0c10] text-white/40">Your app</div>
</StepUpGate>
);
}
return (
<main className="grid min-h-dvh place-items-center bg-[#0b0c10] text-white">
<form
onSubmit={(e) => {
e.preventDefault();
void sendCode();
}}
className="grid w-80 gap-3"
>
<label htmlFor="email" className="text-sm text-white/70">Email</label>
<input id="email" type="email" autoComplete="email" required value={email}
onChange={(e) => setEmail(e.target.value)}
className="rounded-lg bg-white/10 px-3 py-2 outline-none focus:ring-2 focus:ring-[#C6F432]" />
<button className="rounded-lg bg-[#C6F432] px-3 py-2 font-medium text-black">Email me a code</button>
{error ? <p role="alert" className="text-sm text-red-400">{error}</p> : null}
</form>
</main>
);
}
lib/db.ts
import { init } from "@instantdb/react";
export const db = init({ appId: process.env.NEXT_PUBLIC_INSTANT_APP_ID! });
Good to know
Type-checked against @instantdb/react 1.0.67 and next 15.5 with TypeScript 5.9. We haven't run it against a live Instant app.
The code is 6 digits. Instant's server makes it with rand-num-str 6 (server/src/instant/model/app_user_magic_code.clj in the instantdb/instant repo), so the card's 6 cells match without changing length.
A wrong or expired code makes signInWithMagicCode reject. onVerify catches that and returns false, and the cells shake. Instant keeps the message on err.body.message if you want to show it.
Instant creates the user on the first successful code, so this one screen covers sign-up too. Its docs say the same in their example: "We'll create an account for you too if you don't already have one."
The page doesn't switch on db.SignedIn. If it did, the code card would vanish the moment Instant signs the user in and the unlock animation would never play. It moves on in onVerified instead, after the animation.
If you only need a working login and don't care how it looks, the full example on Instant's Magic Code Auth page is free and complete. Paste it and you're done. This guide is for when the code step should feel like part of your app.
For testing, Instant's dashboard can give a test email a static code that never expires, so you don't wait for emails while you build.
Copy StepUpGate.tsx, step-up-gate.css and timeline.ts from react/src/components. The gate frosts whatever you pass as children.
InstantDB magic code in a shadcn app: scope the code card's CSS
InstantDB apps that use shadcn will see the shipped step-up-gate.css restyle more than the magic code screen. We imported it as shipped into a shadcn new-york-v4 test page on Tailwind 4.3.3 on October 25, 2026. The body went near-black and switched to Space Grotesk, the primary Button's label turned dark on its dark fill, Buttons went from 14px/500 to 16px/400 and bg-muted changed colour.
Why: the colour tokens live on :root (two of them, --muted and --font-mono, share names with shadcn and Tailwind), html, body and button get styled directly, and generic classes like .card, .grid and .ring are global. It's all unlayered, so Tailwind's layered utilities lose to it.
Make four edits to your copy and give the gate a class. Your page sits inside the gate as children, so put it in a div with the class otp-backdrop and the rules stop at that div.
.root has to become :scope as well: inside @scope a plain .root doesn't match the root element, and without it the card fell 540px down our test page. The rename matters because custom properties inherit into your page. bg-background text-foreground font-sans on the otp-backdrop div give your page back what it used to get from body. @keyframes shake stays global, so rename it if your app has its own.
After the edits, the shadcn Button, Card, Input, grid, bg-muted strip and link measured the same as on a page without the gate, both outside it and as the page behind it, in headless Chromium 156, Firefox 157 and WebKit 27.2 at 1280 and 390 px. The gate itself only changed at its root, which now carries the dark background and overflow-x: clip html and body used to. The shake still plays. We tested static markup with the real CSS, not the running component.
@scope support, from MDN's compatibility data read October 25, 2026: Chrome and Edge 118+, Firefox 146+, Safari 17.4+. Safari 26.0 to 26.3 has a partial version that skips input elements, and the code cells are inputs, so they'd show as plain fields there. Browsers without @scope drop the whole block. For those, prefix every selector in your copy with .qd-otp instead and move the :root variables onto .qd-otp. That version has no lower edge, so the button reset reaches your page inside the gate, and we haven't measured it.
step-up-gate.css (your copy)
/* step-up-gate.css, your copy. Edits, in this order: */
/* a. delete all 33 "html:not(.fit) " prefixes (max-width: 760px block) */
/* b. ":root{", "html,body{" and ".root{" each become ":scope{" */
/* c. rename --muted to --otp-muted, --font-mono to --otp-font-mono (10 each) */
/* d. wrap everything: */
@scope (.qd-otp) to (.otp-backdrop) {
/* ...rest of the file with a to c applied... */
}
the gate (wrapper)
<StepUpGate className="qd-otp" /* ...the same props as above... */>
{/* the scope stops at this div; the classes give your page its body styles back */}
<div className="otp-backdrop bg-background text-foreground font-sans">
<App />
</div>
</StepUpGate>
Install
Copy from the zipPut react/src/components into src/, then npm i framer-motion @instantdb/react.
Or ask your AIPaste PROMPT.md into Cursor, Lovable or v0.
qodesign vs Aceternity UIAceternity UI is best for: teams building many marketing pages who want one big library.
All comparisons, with prices read on each library's own site.
Questions
Will it change my shadcn styles?
Not if you scope it. As shipped, step-up-gate.css turns a shadcn app dark, switches fonts and breaks Buttons. With the four edits and the qd-otp class on this page, the shadcn parts we measured stayed the same.
Does it work with Instant's guest auth?
Yes. Instant can upgrade a guest to a real user when they sign in with a code. This page only changes the screen, so Instant's guest docs apply as written.
Can I set fields on the new user?
Yes. signInWithMagicCode takes extraFields, and Instant writes them only on the first sign-up. Pass them in the onVerify call.
What about React Native?
The card is a web component. Instant's React Native docs have their own magic code example for Expo.
Does the code screen work in Next.js?
Yes, as a client component. Keep "use client" at the top of the file that renders it, as in the snippet above. App Router and Pages Router both work.
Can Cursor or v0 add it for me?
Yes. Each download has a PROMPT.md. Paste it into Cursor, Lovable, v0 or Claude Code and it fits the piece into your app.
Can I use it in client work?
Yes, in personal and client projects. You can't resell the code itself as a kit.