Skip to content

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.

  • 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.
  1. Pick the page in the table below.
    • Read its default to see the form fields.
  2. Write a function that takes the page input and answers the markup.
  3. Escape every value of the input in the markup.
  4. Keep the form of the default: the same method, action, and field names.
  5. Set the config key of the table to your function.
A confirm page of the app
import {
type ConfirmPageInput,
createAuth,
type MailPort,
type StorageAdapter,
} from "@authtempo/core";
const ESCAPES: Record<string, string> = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#39;",
};
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 },
});
}
The page as a test
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");
});
});
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).
  • 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 email to <basePath>/sso/discover while methods.sso is true. Else an SSO user cannot start a sign-in there. Put continuePath in a hidden next field, 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/discover answers a page of this origin. That page goes on to the start of the connection with a GET. Chromium applies form-action to 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 start with a GET navigation, such as location.href = start. Do not send a form to start, also not a form with method="get": form-action applies to it too.
  • A custom challenge page must read methods, method, trusted, and emailSent of TotpChallengePageInput. 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.
  • 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.