Tutorials

Add passwordless sign-in to a Next.js app

Email a one-time link, verify it on the server and keep a page behind the session.

JoJonas Weber20 minutesIntermediate

Before you start

  • Node.js 20 or later
  • A Next.js 15 app using the App Router
  • An Acme account
1

Install the SDK

Add the auth package to your app. It has no other dependencies.

pnpm add @acme/auth
2

Add your keys

Copy the two keys from your Acme dashboard into the environment file at the root of the project.

.env.local
# Dashboard → Settings → API keysACME_PUBLIC_KEY="pk_live_2f9c41d0"ACME_SECRET_KEY="sk_live_8a61be7742"ACME_REDIRECT_URL="http://localhost:3000/dashboard"

Keep .env.local out of git. The secret key can sign anyone in.

3

Create the auth route

One catch-all route sends the email and verifies the link when the reader clicks it.

app/api/auth/[...acme]/route.ts
import { createAuth } from "@acme/auth"; export const auth = createAuth({ secret: process.env.ACME_SECRET_KEY, redirectTo: process.env.ACME_REDIRECT_URL, session: { maxAge: 60 * 60 * 24 * 30 },}); export const { GET, POST } = auth.handlers;
4

Build the sign-in form

The form posts the email address to a server action. The action asks Acme to send the link.

app/sign-in/page.tsx
import { auth } from "@/app/api/auth/[...acme]/route"; async function sendLink(data: FormData) { "use server"; await auth.sendMagicLink({ email: String(data.get("email")) });} export default function SignIn() { return ( <form action={sendLink}> <input name="email" type="email" autoComplete="email" required /> <button type="submit">Email me a link</button> </form> );}
5

Protect a page

Middleware checks the session before the page renders and sends everyone else to the form.

middleware.ts
import { auth } from "@/app/api/auth/[...acme]/route"; export default auth.protect({ signInUrl: "/sign-in" }); export const config = { matcher: ["/dashboard/:path*"] };
pnpm dev

Check it worked. Open /dashboard signed out and you land on the sign-in form.

Where to go next