Sign in with a magic link
For: an app developer who wants people to sign in with a link by mail.
What it does: core mails a single-use link. The link opens a confirm page, and the button of that page opens a session.
Before you start
Section titled “Before you start”- A working setup from a getting started tutorial.
- A mail transport in
mail.sendof the config. Top level lists themailkey.
- Set
appName. Core puts it in the mail subject and on the confirm page. - Set
magicLink.ttlMinuteswhen the default of 15 minutes does not fit your app. - Set
magicLink.linkBuilderonly when the link needs another form. It must openGET /auth/magic-link/verifywith the token.
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
export function createMagicLinkAuth(deps: { storage: StorageAdapter; mail: MailPort; secret: string;}) { return createAuth({ baseURL: "https://api.example.com", appURL: "https://app.example.com", trustedOrigins: ["https://app.example.com"], secret: deps.secret, storage: deps.storage, mail: deps.mail, // The name in the mail subject ("Sign in to Example") and on the confirm page. appName: "Example", magicLink: { // The life of a link in minutes. The default is 15. ttlMinutes: 10, // The link in the mail. It must open GET /auth/magic-link/verify with the token, on the API. linkBuilder: (token) => `https://api.example.com/auth/magic-link/verify?token=${encodeURIComponent(token)}`, }, });}- From the app, post the address and
nexttoPOST /auth/magic-link/request. - Show the same text for every address. The route answers
{ "sent": true }for an unknown address too. - Tell the person to open the link in the mail.
import { describe, expect, it } from "vitest";import { call, harness, lastMailTo, linkIn } from "../support/harness.ts";import { createMagicLinkAuth } from "./magic-link.ts";
describe("sign in with a magic link", () => { it("mails a link, shows a confirm page with no side effect, and opens a session on the post", async () => { const h = harness(); const auth = createMagicLinkAuth(h); const ask = await call(auth, "POST", "/auth/magic-link/request", { email: "ada@example.com", next: "/home", }); expect(await ask.json()).toEqual({ sent: true }); expect(lastMailTo(h, "ada@example.com").subject).toBe("Sign in to Example");
const link = linkIn(h, "ada@example.com"); for (let i = 0; i < 2; i++) { const page = await auth.handle(new Request(link)); expect(page.status).toBe(200); expect(page.headers.get("set-cookie")).toBeNull(); } const token = link.searchParams.get("token") ?? ""; const verify = await call(auth, "POST", "/auth/magic-link/verify", { token }); expect(verify.status).toBe(200); expect(((await verify.json()) as { user: { email: string } }).user.email).toBe( "ada@example.com", );
const again = await call(auth, "POST", "/auth/magic-link/verify", { token }); expect(await again.json()).toEqual({ error: "invalid_token" }); });
it("answers the same for an address with no account", async () => { const auth = createMagicLinkAuth(harness()); const ask = await call(auth, "POST", "/auth/magic-link/request", { email: "nobody@example.com", }); expect(await ask.json()).toEqual({ sent: true }); });});How it behaves
Section titled “How it behaves”- The link opens a confirm page with one button and no script.
- The button posts the token. Core opens the session, sets the cookie, and sends the browser to
appURLplusnext. - The GET of the link reads no row and counts no limit. A mail scanner that fetches the link spends nothing.
- A link works once. A second post of the same token answers
invalid_token. - A JSON post of the token answers
{ user, session }with the cookie, and never a redirect. A native app uses this form. - A
nextvalue must start with/and not with//. Core replaces any other value with/. - A
nextunderbasePathstays onbaseURL. The handoff uses this rule. - The request has two limits:
magic-link:email:<email>5 per 15 minutes, andmagic-link:ip:<ip>20 per 15 minutes. - A link that proves the address of an unverified account claims it. The claim rule tells what core deletes.
- A user with the second factor on gets a challenge, not a session. Turn on the second factor tells the next step.
If it fails
Section titled “If it fails”invalid_body: the request has no validemail.rate_limited: too many requests for the address or the IP. Wait 15 minutes.invalid_token: the link is spent, malformed, or unknown. Ask for a new link.token_expired: the link is older thanmagicLink.ttlMinutes. Ask for a new link.hook_refused: a session hook of the app refused the sign-in.