Run the app on another origin
For: a developer whose browser app and API live on two origins.
What it does: the router answers the app’s preflight, marks each answer for the app with credentialed CORS, and lands every sign-in on the app.
Before you start
Section titled “Before you start”- A working setup from a getting started tutorial.
- The exact origin of the app, such as
https://app.example.net, with no path and no trailing slash. It goes intrustedOriginsof Top level.
- Set
appURLto the origin of the app. - Add the same origin to
trustedOrigins. Core refuses anappURLthat is notbaseURLor a trusted origin. - For an app on another site, set
cookie: { sameSite: "None" }. - For an app on a sibling subdomain of the API, keep the default,
Lax.
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
export function createSplitOriginsAuth(deps: { storage: StorageAdapter; mail: MailPort; secret: string;}) { return createAuth({ // The API. baseURL: "https://api.example.com", // The app on another site. Every sign-in lands here, and errors go to /?auth_error=<code>. appURL: "https://app.example.net", // The app origin gets credentialed CORS, and its POSTs pass the Origin rule. trustedOrigins: ["https://app.example.net"], // Another site needs SameSite=None, or the browser drops the session cookie. // A sibling subdomain of the API keeps the default, Lax. cookie: { sameSite: "None" }, secret: deps.secret, storage: deps.storage, mail: deps.mail, });}- In the app, make the client with the default
credentials: "include", so that the browser sends the cookie.
import { describe, expect, it } from "vitest";import { call, harness, linkIn } from "../support/harness.ts";import { createSplitOriginsAuth } from "./split-origins.ts";
const APP = "https://app.example.net";
describe("run the app on another origin", () => { it("answers the app's preflight with credentialed CORS, and refuses another origin", async () => { const auth = createSplitOriginsAuth(harness()); const ok = await call(auth, "OPTIONS", "/auth/session", undefined, { origin: APP }); expect(ok.status).toBe(204); expect(ok.headers.get("access-control-allow-origin")).toBe(APP); expect(ok.headers.get("access-control-allow-credentials")).toBe("true"); const preflight = await call(auth, "OPTIONS", "/auth/session", undefined, { origin: "https://evil.example", }); expect(preflight.status).toBe(403); expect(await preflight.json()).toEqual({ error: "origin_not_trusted" }); const other = await call( auth, "POST", "/auth/magic-link/request", { email: "a@example.com" }, { origin: "https://evil.example", }, ); expect(other.status).toBe(403); expect(await other.json()).toEqual({ error: "origin_not_trusted" }); });
it("lands the sign-in on the app origin with a SameSite=None cookie", async () => { const h = harness(); const auth = createSplitOriginsAuth(h); await call( auth, "POST", "/auth/magic-link/request", { email: "ada@example.com", next: "/home" }, { origin: APP }, ); const token = linkIn(h, "ada@example.com").searchParams.get("token") ?? ""; const res = await auth.handle( new Request("https://api.example.com/auth/magic-link/verify", { method: "POST", headers: { "content-type": "application/x-www-form-urlencoded", origin: "https://api.example.com", }, body: new URLSearchParams({ token }).toString(), }), ); expect(res.headers.get("location")).toBe(`${APP}/home`); expect(res.headers.get("set-cookie")).toContain("SameSite=None"); });});How it behaves
Section titled “How it behaves”- A trusted origin gets a 204 answer to its preflight. Any other origin gets 403.
- A POST with an
Originheader that is not trusted answers 403 before any handler runs. - A POST with no
Originheader is a client that is not a browser, and passes. - A form post, multipart,
text/plain, or a post with no content type needsSec-Fetch-Siteto besame-originornone. A JSON post withSec-Fetch-Siteset tocross-siteorsame-siteneeds a trustedOrigin. Each refusal answers 403origin_not_trusted, and a request with noSec-Fetch-Sitepasses. - With
SameSite=None, any site can make the browser send the cookie. The Origin rule is what refuses such a request. - A
nextunderbasePathstays onbaseURL. Every othernextlands onappURL. - The cookie names are
__Host-tempo_sessionand__Secure-tempo_session_oauth. The session cookie is a__Host-cookie, so a host on a sibling subdomain cannot set it.
If it fails
Section titled “If it fails”origin_not_trusted: theOriginheader is notbaseURLand not intrustedOrigins, or the browser marks a form post as not same-origin. Post JSON, or post the form frombaseURL.- The client answers
networkwhen the browser blocks an answer for an origin that is not trusted. client.session()answersnullon another site when the cookie isLax. SetSameSite=None.