Serve one deployment on several hosts
For: an app developer who serves the auth routes of one deployment on two or more hosts, such as a staging and a production host.
What it does: baseURL takes a list of exact origins. The router picks the entry equal to the origin of the request URL. Every URL that core builds follows that entry.
Before you start
Section titled “Before you start”- A working deployment from Start.
- The list of hosts, each an exact origin as the
baseURLrow states. Write it as the URL parser does: lower case, a host name in punycode, and no default port.
- Set
baseURLto the list. The first entry is the primary. - Register each host’s callback at each social provider:
<entry><basePath>/social/<provider>/callback. - On Node, build
request.urlfrom the host that your proxy checked. Core reads the host fromrequest.urlonly. - Express reads the host from the Host header, and with
trust proxythe scheme fromX-Forwarded-Proto. So your proxy must pass the original Host header to Express. Fastify withtrustProxyreadsX-Forwarded-HostandX-Forwarded-Proto. Then your proxy must set both headers on each request and drop the client’s values. A client can still pick only an entry of the list.
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
/** One deployment that serves two hosts. The first entry is the primary. */export function createTwoHostAuth(deps: { storage: StorageAdapter; mail: MailPort; secret: string;}) { return createAuth({ // Exact origins only: no wildcard, no path, https (or http on localhost). baseURL: ["https://auth.example.com", "https://auth.example.org"], trustedOrigins: [], secret: deps.secret, storage: deps.storage, mail: deps.mail, oauth: { resources: ["https://api.example.com/mcp"], registration: { cimd: false } }, });}import { describe, expect, it } from "vitest";import { harness } from "../support/harness.ts";import { createTwoHostAuth } from "./base-url-list.ts";
describe("serve one deployment on a list of hosts", () => { it("names the issuer of the request's host", async () => { const auth = createTwoHostAuth(harness()); for (const host of ["https://auth.example.com", "https://auth.example.org"]) { const res = await auth.handle(new Request(`${host}/auth/.well-known/openid-configuration`)); expect(((await res.json()) as { issuer: string }).issuer).toBe(`${host}/auth`); } });
it("answers 421 to a host outside the list, whatever the forwarded headers say", async () => { const auth = createTwoHostAuth(harness()); const res = await auth.handle( new Request("https://evil.example/auth/session", { headers: { "x-forwarded-host": "auth.example.com" }, }), ); expect(res.status).toBe(421); expect(await res.json()).toEqual({ error: "host_not_allowed" }); });});How it behaves
Section titled “How it behaves”- Core compares the origin of
request.urlwith each entry, exactly. The scheme, the host, and the port must each be equal. A host in upper case is the same host, because the URL parser lowers it. - Core never reads
x-forwarded-host,x-forwarded-proto, orforwarded. A client sets each of them, so a header cannot pick a host. - A host outside the list gets 421
host_not_allowedbefore any handler. The answer holds the code only.auth.wellKnownanswers the same, except for the WebAuthn related origins document. - These follow the picked entry: the OAuth issuer, the metadata documents, the mail links, and the social callback. So do the failure redirect, the default
appURL, theOrigincheck, and the defaultjwtMint.audience. - A
redirectURIoverride must be on an entry of the list. The start sends its path on the entry where the sign-in started, so the sign-in ends on that host. - The session cookie has the
__Host-prefix and noDomain. Each host keeps its own session. The bag cookie ofmultiSessionfollows the same rule. - The Express, Fastify, and Hono bindings serve a request whose
Hostheader does not parse as the primary entry. That is no way past the 421: such a client can send the primary host too. - A session JWT of any entry verifies on each entry, with the audience of the entry that minted it.
- A back-channel sign-out message names the issuer of the entry where the client got its ID token. It keeps that issuer also when the entry left the list, because the client refuses any other issuer.
- An OAuth code or a held authorize request of one entry answers
invalid_grant, or is not found, on another entry. RFC 8414 binds an issuer to the host of its metadata. - Outside a request, core uses the primary:
auth.baseURL, the sweep, and eachauth.apicall. - A
baseURLstring serves any host, as before. The 421 answer needs a list. - The Better Auth shim maps
{ allowedHosts, fallback, protocol }to the list. It refuses a wildcard host, because any customer of a shared platform can own a matching host. It refusesprotocol: "auto"and noprotocol, becauseautoreads a proxy header. - The shim serves its own paths, the SSO URLs, and the mail links on the request’s entry. A host outside the list gets 421
HOST_NOT_ALLOWEDbefore any shim path, the SAML ACS too. - These stay on the primary: the default
appName, the TOTP issuer label, and the OpenAPIserverUrl. SetappNameto give each host one name. - A refresh token and a device code are not bound to an entry. A refresh on another entry gives no ID token.
- Each host keeps its own session, but a device code flow can let one session get ID tokens on two entries. A client then sees two issuers, and the back-channel message names only the first.
- An SSO provider must register the callback or ACS of each entry where a sign-in starts.
If it fails
Section titled “If it fails”host_not_allowed: the request URL has an origin that is not in the list. Add the origin, or fix the URL that the adapter builds.origin_not_trusted: a page on one entry posted to another entry. Each entry trusts its own origin only. Add the other entry totrustedOrigins.invalid_grant: an OAuth client sent a code to the token endpoint of another entry. Send it to the issuer that the client read.