Sign-in handoff flow
Step-by-step Account handoff from a protected page to a host-only session cookie.
Sign-in handoff flow
How a user on a Ziteox site (e.g. Client at client.ziteox.com) ends up with a session on that site after signing in at Account (auth.ziteox.com). Admin uses the same pattern with a stricter staff / two-step policy.
Sequence
sequenceDiagram
participant Browser
participant Site as Site (e.g. client.ziteox.com)
participant Account as Ziteox Account (auth.ziteox.com)
participant Supabase
Browser->>Site: Hit protected page
Site->>Browser: Redirect to /sign-in or /auth/start?return_to=…
Browser->>Site: GET /auth/start?return_to=…
Note over Site: Create state + PKCE verifier<br/>Store in zx_<app>_handoff cookie (10 min)
Site->>Browser: 303 → Account /handoff?client_id&redirect_uri&state&code_challenge&return_to
Browser->>Account: GET /handoff
Note over Account: Site registered? redirect_uri exact match?
Account->>Supabase: Login (+ two-step / staff when required)
Account->>Browser: Continue screen / account picker
Browser->>Account: Continue
Note over Account: Issue one-time code (hash stored, 60s, single use)
Account->>Browser: Redirect → Site /auth/handoff?code&state
Browser->>Site: GET /auth/handoff?code&state
Note over Site: Match state to handoff cookie
Site->>Account: POST /api/handoff/token (code, code_verifier, redirect_uri, client_secret)
Account->>Site: Session tokens (+ record Connected apps)
Note over Site: Set host-only session cookie<br/>Clear handoff cookie<br/>Re-check staff / two-step rules
Site->>Browser: Redirect to return_toSteps
- Protected page — Middleware finds no usable session and sends the browser to
/sign-in?return_to=…(or straight to/auth/startwhen a session exists but Admin needs a fresh two-step). /auth/start— Site creates randomstateand a PKCEcode_verifier, stores{ state, verifier, returnTo }in a short-lived httpOnly handoff cookie (zx_admin_handofforzx_client_handoff, path/auth, maxAge 600 seconds / 10 minutes), then 303 redirects to Account/handoffwith:client_id(adminorclient)redirect_uri(exact registered callback, e.g.https://client.ziteox.com/auth/handoff)statecode_challenge(S256 of the verifier) andcode_challenge_method=S256return_to(sanitized relative path)
- Account
/handoff— Loads the site fromcore.apps. Rejects unknownclient_idor aredirect_urithat is not an exact registered callback (origin must also be inallowed_origins). Unauthenticated users go through login (and MFA / staff checks when the site requires them). - Continue — Account picker / Continue screen. On continue, Account issues a one-time code: random, stored only as a SHA-256 hash, valid 60 seconds, single use, bound to the PKCE challenge,
client_id, andredirect_uri. Browser redirects to<site>/auth/handoff?code=…&state=…. - Site
/auth/handoff— Checksstateagainst the handoff cookie (constant-time). Server-to-serverPOSTto Account/api/handoff/tokenwithgrant_type=authorization_code,code,code_verifier,redirect_uri,client_id, andclient_secret(from that site’sZITEOX_SITE_SECRET). Account verifies everything and returns session tokens; it also upsertscore.app_connections(Connected apps). - Finish — Site calls
setSession, sets its own host-only cookie (sb-zx-admin-auth-tokenorsb-zx-client-auth-token), clears the handoff cookie, re-checks staff / two-step rules, then redirects toreturn_to.
Return-to rules
Handoff uses relative paths only. Site helpers (sanitizeAdminHandoffReturnTo / sanitizeClientHandoffReturnTo) call safeReturnTo with an empty origin allowlist and fallback /dashboard.
| Input | Result |
|---|---|
Same-origin path like /projects?tab=1 or /dashboard/users | Accepted as-is |
| Empty / missing | /dashboard |
Full URL (https://…) | Fallback /dashboard |
Protocol-relative (//evil.example) | Fallback /dashboard |
Backslash or %5c tricks | Fallback /dashboard |
javascript: / data: / other non-http(s) schemes | Fallback /dashboard |
On Account’s authorize page, a non-relative return_to fails param validation (invalid_return_to) instead of silently falling back.
When it fails
If the code is missing, expired, already used, state mismatches, the secret is wrong, or post-session gates fail:
- Best-effort local sign-out on that site (
scope: "local") - Clear the handoff cookie
- Return a static HTML page (Admin heading: “Sign-in didn't finish”; Client uses expired-link wording) with a link back to
/sign-in
There is no automatic redirect back to /auth/start, so a bad handoff cannot loop.
Sign-out
Site /auth/signout
| Method | Behaviour |
|---|---|
| POST | Sign out only this site’s session (scope: "local"), clear the handoff cookie, 303 to /sign-in. Use a real HTML form so browsers do not prefetch it. |
| GET | 303 to /sign-in only — does not sign anyone out (prefetch / bookmark safe). |
Local site sign-out does not clear the Account session on auth.ziteox.com.
Account /logout
GET /logout on Ziteox Account calls signOut({ scope: "global" }) (revokes refresh tokens globally), then redirects using safeReturnTo on returnTo (fallback /login).