Replace the default pages
For: an app developer who wants core’s pages in the look of the app.
What it does: a page function of the config answers the markup. Core sets the headers and serves the page.
Before you start
Section titled “Before you start”- A working setup from a getting started tutorial.
- The default page of the same kind, to copy its form fields. Core exports each default, as the Functions table lists.
- Pick the page in the table below.
- Read its default to see the form fields.
- Write a function that takes the page input and answers the markup.
- Escape every value of the input in the markup.
- Keep the form of the default: the same
method,action, and field names. - Set the config key of the table to your function.
import { type ConfirmPageInput, createAuth, type MailPort, type StorageAdapter,} from "@authtempo/core";
const ESCAPES: Record<string, string> = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'",};const escapeHtml = (s: string) => s.replace(/[&<>"']/g, (c) => ESCAPES[c] ?? c);
/** * The confirm page of a magic link. It must hold a form that posts `token` to * `action`. Core sets the headers: no script runs, and the form posts to this * origin only. Return the markup only. */export function confirmPage(input: ConfirmPageInput): string { return `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Sign in to ${escapeHtml(input.appName)}</title><style>body{font-family:system-ui,sans-serif;max-width:28rem;margin:4rem auto;padding:0 1rem}</style></head><body><h1>Sign in to ${escapeHtml(input.appName)}</h1><form method="post" action="${escapeHtml(input.action)}"><input type="hidden" name="token" value="${escapeHtml(input.token)}"><button type="submit">Continue to ${escapeHtml(input.appName)}</button></form></body></html>`;}
export function createPagesAuth(deps: { storage: StorageAdapter; mail: MailPort; secret: string }) { return createAuth({ baseURL: "https://api.example.com", trustedOrigins: [], appName: "Example", secret: deps.secret, storage: deps.storage, mail: deps.mail, magicLink: { confirmPage }, });}import { describe, expect, it } from "vitest";import { call, harness, linkIn } from "../support/harness.ts";import { createPagesAuth } from "./pages.ts";
describe("replace the default pages", () => { it("serves the app's confirm page with core's headers", async () => { const h = harness(); const auth = createPagesAuth(h); await call(auth, "POST", "/auth/magic-link/request", { email: "ada@example.com" }); const page = await auth.handle(new Request(linkIn(h, "ada@example.com"))); expect(await page.text()).toContain('<button type="submit">Continue to Example</button>'); expect(page.headers.get("cache-control")).toBe("no-store"); expect(page.headers.get("content-security-policy")).toContain("form-action"); expect(page.headers.get("x-robots-tag")).toBe("noindex"); });});The pages
Section titled “The pages”| Config key | Default | The page |
|---|---|---|
magicLink.confirmPage |
defaultConfirmPage |
The page of a magic link, with one button that posts the token. |
handoff.signInPage |
defaultSignInPage |
The sign-in page of the handoff and of the OAuth authorize step. |
handoff.confirmPage |
defaultHandoffConfirmPage |
The page that names the account and the app before the code is minted. |
password.resetPage |
defaultResetPage |
The form of a reset link. |
totp.challengePage |
defaultTotpPage |
The form of a second factor challenge. |
oauth.consentPage |
defaultConsentPage |
The consent page of the OAuth provider. |
oauth.devicePage |
defaultDevicePage |
The device page of the OAuth provider: the code form, the consent, and the end (RFC 8628). |
How it behaves
Section titled “How it behaves”- Core sets
cache-control: no-store,referrer-policy: strict-origin,x-robots-tag: noindex, and a content security policy. - The policy permits inline style, no script, and a form post to this origin only. A page that needs script needs its own route in the app.
- The sign-in page is the one page with a script: the passkey ceremony, under a nonce. A custom sign-in page must put the nonce on its script tag.
- A custom sign-in page must show
methods.providers, or the buttons of GitHub, LinkedIn, and the list providers do not show. - A custom sign-in page must show a form that posts
emailto<basePath>/sso/discoverwhilemethods.ssois true. Else an SSO user cannot start a sign-in there. PutcontinuePathin a hiddennextfield, so the sign-in lands on it. The default page has the form “Sign in with SSO” with no script. - The form post of
/sso/discoveranswers a page of this origin. That page goes on to the start of the connection with a GET. Chromium appliesform-actionto each redirect after a form post, and the origin of the sign-in provider is not in the policy. So a redirect chain from the post to the provider stops in Chromium. - A custom page with a script that reads the JSON answer must go to
startwith a GET navigation, such aslocation.href = start. Do not send a form tostart, also not a form withmethod="get":form-actionapplies to it too. - A custom challenge page must read
methods,method,trusted, andemailSentofTotpChallengePageInput. Without them the page shows no email form and no trusted-device button, and a person who signs in with Apple must type a code. - A challenge whose only method is a passkey needs a page with a script. The default page then shows the backup code form, which passes only while a code is left.
- The verify page of a password sign-up has no config key. Core serves
defaultPasswordVerifyPage.
If it fails
Section titled “If it fails”- A page with a script shows no error, and the browser does not run the script. Move the script to a route of the app.
- A form with other field names posts nothing that core reads, and the flow answers
invalid_token.