Skip to content

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.

  1. Set appName. Core puts it in the mail subject and on the confirm page.
  2. Set magicLink.ttlMinutes when the default of 15 minutes does not fit your app.
  3. Set magicLink.linkBuilder only when the link needs another form. It must open GET /auth/magic-link/verify with the token.
The magic link config
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)}`,
},
});
}
  1. From the app, post the address and next to POST /auth/magic-link/request.
  2. Show the same text for every address. The route answers { "sent": true } for an unknown address too.
  3. Tell the person to open the link in the mail.
The flow as a test
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 });
});
});
  • 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 appURL plus next.
  • 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 next value must start with / and not with //. Core replaces any other value with /.
  • A next under basePath stays on baseURL. The handoff uses this rule.
  • The request has two limits: magic-link:email:<email> 5 per 15 minutes, and magic-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.
  • invalid_body: the request has no valid email.
  • 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 than magicLink.ttlMinutes. Ask for a new link.
  • hook_refused: a session hook of the app refused the sign-in.