Guides/Production patterns
Share sign-in across applications
One session for several subdomains, or for a second application that reaches sign-in through a rewrite.
The problem
Session cookies are host-only by default: a cookie set by app.example.com is not sent to admin.example.com. When several applications should share one sign-in, either the cookie has to cover all of them, or every application has to reach the auth server from its own origin.
When to use it
- Subdomains of one domain, for example one subdomain per organization or a separate admin host: share the cookie across the parent domain.
- An application on another host, or one that should not hold the auth secret or a database connection: rewrite its
/api/auth/*to the application that serves sign-in.
Subdomains of one domain
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
baseURL: "https://app.example.com",
advanced: {
crossSubDomainCookies: {
enabled: true,
domain: "example.com",
},
},
trustedOrigins: ["https://*.example.com"],
});- Set
domainto the parent domain. Without it the cookie's domain is the host ofbaseURL(hereapp.example.com), which does not reach sibling subdomains. https://*.example.comtrusts every HTTPS subdomain and refuses the same hosts overhttp://. See Wildcard Origins.- Every subdomain receives the session cookie. Keep hosts that run code you do not control on another domain, as the cookie warning says.
- The session hint cookie takes the session cookie's domain, so it is shared the same way.
A second application through a rewrite
The second application sends /api/auth/* to the application that serves sign-in, so the browser only ever talks to its own origin:
{
"rewrites": [
{
"source": "/api/auth/:path*",
"destination": "https://app.example.com/api/auth/:path*"
}
]
}The sign-in application lists the second one as a trusted origin:
export const auth = entroAuth({
baseURL: "https://app.example.com",
trustedOrigins: ["https://console.example.net"],
});- The session cookie is set host-only for the second application's host, with its
__Secure-name on HTTPS. Cookies are host-only by default, so this session is the second host's own and is not shared with the sign-in application's host. - The second application needs no auth secret and no database connection. Its client uses its own origin, and its server reads the session by asking
get-sessionwith the request's cookies, as the Next.js middleware example does:
export async function getSession(request: Request) {
const response = await fetch("https://app.example.com/api/auth/get-session", {
headers: { cookie: request.headers.get("cookie") ?? "" },
});
if (!response.ok) throw new Error(`get-session answered ${response.status}`);
return response.json(); // null when nobody is signed in
}Links and callbacks that the sign-in application builds, such as a social provider's callback or a verification link, use its own baseURL: a static baseURL takes precedence over forwarded headers. If those flows have to land on the second host, read Dynamic Base URL and Trusted Proxy Headers first.
A rewrite is also the way around Safari's tracking prevention when the auth server is on another site. See Safari, ITP, and Cross-Domain Setups.
Pitfalls
localhostcannot share a cookie with its subdomains. Browsers keepDomain=localhostto the one host. A wildcard name that resolves to127.0.0.1, such aslvh.me, works for local testing, with two catches: Next.js blocks its development resources on a host it was not told about (allowedDevOrigins), and plain HTTP on such a name is not a secure context, socrypto.randomUUIDis missing in the browser.- Test
trustedOriginswith a session cookie. The origin of a request without cookies is not always checked: a POST from any origin with no session can reach an endpoint and be answered on its merits. With a session cookie, a foreign origin is refused withINVALID_ORIGIN. A probe that sends no cookie proves nothing about the list. See CSRF Protection. - The origin check is off under
NODE_ENV=test. Tests of your owntrustedOriginspass whatever the list says unless the configuration they build setsadvanced.disableOriginCheck: false. SeedisableOriginCheck. - Do not leave
http://localhostorigins in a production list.
Reference
- Cookies: prefixes, cross-subdomain cookies, secure cookies
- Trusted Origins
trustedOriginsoption