Skip to content

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.

  • 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 in trustedOrigins of Top level.
  1. Set appURL to the origin of the app.
  2. Add the same origin to trustedOrigins. Core refuses an appURL that is not baseURL or a trusted origin.
  3. For an app on another site, set cookie: { sameSite: "None" }.
  4. For an app on a sibling subdomain of the API, keep the default, Lax.
The config of two origins
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,
});
}
  1. In the app, make the client with the default credentials: "include", so that the browser sends the cookie.
The CORS answers as a test
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");
});
});
  • A trusted origin gets a 204 answer to its preflight. Any other origin gets 403.
  • A POST with an Origin header that is not trusted answers 403 before any handler runs.
  • A POST with no Origin header is a client that is not a browser, and passes.
  • A form post, multipart, text/plain, or a post with no content type needs Sec-Fetch-Site to be same-origin or none. A JSON post with Sec-Fetch-Site set to cross-site or same-site needs a trusted Origin. Each refusal answers 403 origin_not_trusted, and a request with no Sec-Fetch-Site passes.
  • With SameSite=None, any site can make the browser send the cookie. The Origin rule is what refuses such a request.
  • A next under basePath stays on baseURL. Every other next lands on appURL.
  • The cookie names are __Host-tempo_session and __Secure-tempo_session_oauth. The session cookie is a __Host- cookie, so a host on a sibling subdomain cannot set it.
  • origin_not_trusted: the Origin header is not baseURL and not in trustedOrigins, or the browser marks a form post as not same-origin. Post JSON, or post the form from baseURL.
  • The client answers network when the browser blocks an answer for an origin that is not trusted.
  • client.session() answers null on another site when the cookie is Lax. Set SameSite=None.