Unified Login Across Independently Hosted Domains Using OIDC
System DesignModel answerLast updated
Design a shared identity layer that lets users sign in once and move freely between two separate domains, then scale the same pattern to many sites and delegated API access.
Two products live on entirely separate domains: atlas.app.io and compass.app.io. Neither is a subdomain of the other, so they cannot share a browser cookie. A user who logs into one should be able to navigate to the other without being asked to enter their password again. Your job is to design the system that makes that happen.
Walk through the mechanics from first principles: what shared component enables the seamless transition, how the browser flow actually works, and what each site ends up holding at the end. Be specific enough that an engineer could implement it.
Then generalize: the company will eventually operate dozens of sites on the same pattern, and some of those sites need to call third-party APIs (a project tracker, a data warehouse, a messaging platform) on behalf of the signed-in user. Describe how to extend the design to cover both cases, and be precise about the difference between a user being recognized by a third-party tool and the application being permitted to call that tool's API.
Constraints
atlas.app.ioandcompass.app.ioare distinct registrable domains. Cookies set on one are not readable by the other.- Each site can redirect the browser to another domain and can handle a callback from that domain on the server side.
- "No repeated login" means no second password prompt as long as the shared identity session is still alive. It does not require any direct communication between the two sites.
- You may not rely on cross-site iframes reading another site's cookies; that approach is blocked by modern browsers.
compass.app.iohas its own existing user database. Account migration or linking must be addressed.
What to cover
Part 1 — The two-site flow
Walk through a user's first login on atlas.app.io and their later visit to compass.app.io. Include:
- Why a shared identity provider (IdP) solves the cross-domain problem
- The full authorization-code exchange: redirects, state/PKCE binding, token validation, and how each site forms its own local session
- What happens when the IdP's own session has already expired by the time the user reaches the second site
Part 2 — Many sites and delegated API access
- How the two-site pattern scales to an arbitrary number of sites without changing its shape
- How the application decides whether a given user is allowed to access a given third-party tool, and who can revoke that
- The difference between federating a user's identity into a third-party tool versus obtaining an OAuth access token to call that tool's API — and why an ID token is the wrong credential for the second case
Hints
Hint 1
The two sites don't need to communicate with each other at all. Think about what a shared identity provider already knows by the time the second site redirects to it — and what that means for the user experience.
Hint 2
Trace the authorization-code flow all the way through: what the browser carries, what stays server-to-server, what each party validates, and what credential the site actually uses as its ongoing session. The IdP tokens and the local session are two separate things.
Hint 3
When it comes to third-party tools, ask whether the user is the subject of the action or whether the application is acting autonomously. That distinction maps directly onto which OAuth flow and which token type belongs in each scenario.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Part 1 — Cross-domain login flow
Why a shared IdP solves the problem
The fundamental issue is that cookies are scoped to a domain, so atlas.app.io and compass.app.io can never share a session cookie directly. The solution is to stop trying: instead of the two sites sharing anything with each other, both of them delegate authentication to a third domain — the identity provider — which has its own session cookie scoped to its own domain. Neither product site needs to see that cookie. They just need to redirect to the IdP and trust its verdict.
"No repeated login" doesn't come from the sites cooperating; it comes from the second site arriving at the IdP with the IdP's session already active.
The flow, step by step
First login on atlas.app.io:
- An unauthenticated user hits
atlas.app.io. The server redirects the browser to the IdP's authorization endpoint, attaching astateparameter (a random, unguessable value stored server-side for CSRF protection) and a PKCEcode_challenge. - The user authenticates at the IdP. The IdP sets its own session cookie (on its own domain) and redirects back to
atlas.app.io/callback?code=...&state=.... atlas.app.io's backend verifies thestatevalue matches what it stored, then makes a server-to-server POST to the IdP's token endpoint with the authorization code and the PKCEcode_verifier.- The IdP returns an ID token and, optionally, an access token. The backend validates the ID token: check the signature against the IdP's published JWKs, confirm
iss,aud,exp, andnonce. - The site creates its own local session — a
Set-Cookieonatlas.app.io— derived from the validated claims. The IdP tokens themselves are not used as the ongoing session credential; they've done their job.
Later visit to compass.app.io:
- The user navigates to
compass.app.io. That server redirects to the same IdP authorization endpoint with its own freshstateand PKCE challenge. - The browser sends the IdP's session cookie along with the request. The IdP recognizes the active session, skips the login form, and immediately redirects back to
compass.app.io/callbackwith a new authorization code. compass.app.ioruns the identical code exchange and local session creation steps.
From the user's perspective: no password prompt. Under the hood: two fully independent authorization-code exchanges and two separate local sessions, one per domain. That isolation is intentional — a compromised session on one site cannot be used to impersonate the user on the other.
PKCE is essential here. Without it, a leaked authorization code can be exchanged by anyone. The code_verifier/code_challenge pair ensures only the initiating server can complete the exchange.
When the IdP session has expired
If the IdP's session cookie is gone by the time the second site redirects there, the IdP falls back to a standard login prompt. This is correct behavior, not a gap in the design. SSO guarantees "no second prompt while the IdP session is valid," not unconditional single sign-in.
For sensitive operations requiring higher assurance (accessing financial data, changing credentials), the site can include acr_values or prompt=login in its authorization request to force reauthentication regardless of session state.
Migrating compass.app.io's existing accounts
Two realistic paths: hard migration (bulk-move credentials to the IdP, then retire the local password store) or progressive linking (the first time a returning compass.app.io user goes through the new flow, prompt them to confirm their identity with their old credentials, then write a durable link between the IdP subject ID and their existing account). Progressive linking keeps the migration invisible to most users but requires a short window of running two credential systems in parallel.
Part 2 — Scaling to many sites and delegated API access
Many sites
Each new site added to the platform repeats the exact same pattern against the same IdP: redirect, code exchange, validate, create a local session. No site needs to know about the others. The IdP is the only shared component, and its interface to each site is identical. Adding a 50th site is no different architecturally than adding the second one.
Federating a user's identity into a third-party tool
When the goal is for the user to be recognized by a third-party tool — meaning the tool's own UI shows them as logged in — the tool becomes an OIDC relying party registered against the same IdP, or the application passes a signed attestation the tool trusts. The user ends up with their own session on the tool, created through the same authorization-code flow described above. This is identity federation.
Obtaining delegated API access (this is different)
When the goal is for the application to call the tool's API autonomously — posting a comment, reading a file, querying a data warehouse — that is OAuth authorization, not identity federation, and it requires an access token scoped to the specific permissions needed.
The application registers as an OAuth client with the tool, initiates its own authorization-code flow (with the user present to grant consent), receives an access token and a refresh token, and stores the refresh token securely. Future API calls use fresh access tokens obtained from the refresh token, with no user interaction required.
Why an ID token is wrong here. An ID token asserts who the user is to the application that received it. It is not a bearer credential for downstream APIs — it has the wrong audience, a short expiry, and carries no scoped permissions for the target API. Passing an ID token to a third-party API either fails (if the API validates aud) or creates a confused deputy situation where the tool can't tell whether the token was intended for it. Always use a purpose-issued access token for API calls.
Access control and revocation
Keep the entitlement decision centralized in the application, not spread across the IdP and each tool. The application maintains a per-user, per-tool permission record (driven by roles, group membership, or explicit admin grants), checks it before initiating any authorization flow with a new tool, and is the authoritative place an admin revokes access.
Revocation in the application must propagate outward: call the tool's token revocation endpoint so the access token actually stops working, not just the user's ability to initiate new flows from the UI.
Trade-offs worth stating
- A shared IdP session is more valuable to an attacker than any individual site's session, since compromising it affects all sites. Step-up authentication bounds this risk for sensitive actions.
- Centralizing entitlement in the application simplifies administration but makes the application the mandatory choke point — if it fails to propagate a revocation, the user retains access at the tool level until the token expires.
- Storing refresh tokens for tool API access requires treating them like credentials: encrypted at rest, rotated on use, and audited.