Single sign-on across subdomains
Sign in on my.example.com and you are signed in on example.com, studio.example.com and every other subdomain of example.com, in the browser and in server rendering. Nothing to configure.
How it works
@djangocfg/api keeps the JWT pair in two cookies instead of localStorage:
| Cookie | Holds | Lifetime |
|---|---|---|
cfg.auth.access_token | access token | 30 days (the token itself expires sooner, the API decides) |
cfg.auth.refresh_token | refresh token | until the refresh token’s own exp |
Both are written with Path=/, SameSite=Lax, Secure on https, and Domain= the registrable domain of the page. The client finds that domain by asking the browser (it tries example.com, then longer names, and keeps the first one the browser accepts), so co.uk, vercel.app and other public suffixes need no list on our side. On localhost, an IP or a single-label host the cookie stays host-only.
The API key, OAuth state, redirect URL and drafts are not shared; they stay in localStorage / sessionStorage of each origin.
Users signed in before the upgrade keep their session: on first load the client copies a pair found in localStorage into the cookies and removes it.
Want one place to sign in as well (a dedicated auth. host, one Google redirect URI)? See One sign-in host.
Sign-out
auth.clearTokens() expires the cookie on the shared domain and its host-only twin, and clears localStorage too. Other subdomains notice on their next focus (focus, visibilitychange, and cookieStore change events where the browser has them); a tab already looking at the page updates at once where cookieStore exists.
Starting OAuth from any subdomain
Declare one origin (site_url="https://carapis.com") and the OAuth / MCP-consent gate accepts my.carapis.com, studio.carapis.com and the apex too, the same rule as hosts, CORS and CSRF. Declaring my.carapis.com accepts carapis.com as well. Scheme and port must match; api.*/mcp.* hosts, lookalikes (carapis.com.evil.io) and other sites are refused. accept_subdomains=False makes it exact.
This opens django-cfg’s gate only. Google and GitHub still accept exact redirect URIs: register https://<origin>/auth?provider=<id> for each origin people sign in from (manage.py oauth_check prints the ones accepted by the rule but not registered by default). A project that listed the apex in security_domains only for sign-in can drop it.
Server rendering
The cookie is visible to the server, so Server Components and route handlers on any subdomain can call the API as the user:
import { serverAuthHeaders } from '@djangocfg/nextjs/auth';
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/cfg/accounts/profile/`, {
headers: await serverAuthHeaders(), // { Authorization: 'Bearer …' } or {}
cache: 'no-store',
});getServerAccessToken(), getServerRefreshToken() and hasServerSession() are also exported. Without Next.js, parse the header yourself with readCookie(cookieHeader, CFG_KEYS.accessToken.key) from @djangocfg/api/storage.
Backend
Nothing new beyond subdomain acceptance: the API authenticates the Authorization header only and never reads these cookies, so they open no CSRF surface. CORS lets every subdomain of a declared site send the header.
Options (optional, the defaults need none)
import { auth } from '@djangocfg/api';
auth.setCookieDomain(null); // host-only cookies: no sharing between subdomains
auth.setCookieDomain('example.com'); // force a domain
auth.getCookieDomain(); // what is used now ('example.com' | null)
auth.setStorageMode('localStorage'); // opt out of cookies entirely (once, at bootstrap)Limits and security
- Different registrable domains are different sessions (
caribbeanrealestatemls.comandmlsbali.comdo not share). Cross-domain sign-in would need a redirect handshake and is not part of this. - The cookie is readable by page scripts and by every subdomain, like
localStorageis by the page. A subdomain you do not control (takeover, an untrusted tenant) can read it. Keep untrusted tenants off the domain. - Two tokens add about 1.5 KB to every request to the domain family, static hosts included.
Upgrading from localStorage tokens
Before @djangocfg/api 2.1.604 the tokens lived in localStorage. After the upgrade the provider drops those entries and the cached profile on the first load without a cookie session, so a user who was signed in the old way lands on the sign-in page instead of an empty private area. Nothing to configure.