SystemsAuthentication flow

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_to

Steps

  1. Protected page — Middleware finds no usable session and sends the browser to /sign-in?return_to=… (or straight to /auth/start when a session exists but Admin needs a fresh two-step).
  2. /auth/start — Site creates random state and a PKCE code_verifier, stores { state, verifier, returnTo } in a short-lived httpOnly handoff cookie (zx_admin_handoff or zx_client_handoff, path /auth, maxAge 600 seconds / 10 minutes), then 303 redirects to Account /handoff with:
    • client_id (admin or client)
    • redirect_uri (exact registered callback, e.g. https://client.ziteox.com/auth/handoff)
    • state
    • code_challenge (S256 of the verifier) and code_challenge_method=S256
    • return_to (sanitized relative path)
  3. Account /handoff — Loads the site from core.apps. Rejects unknown client_id or a redirect_uri that is not an exact registered callback (origin must also be in allowed_origins). Unauthenticated users go through login (and MFA / staff checks when the site requires them).
  4. 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, and redirect_uri. Browser redirects to <site>/auth/handoff?code=…&state=….
  5. Site /auth/handoff — Checks state against the handoff cookie (constant-time). Server-to-server POST to Account /api/handoff/token with grant_type=authorization_code, code, code_verifier, redirect_uri, client_id, and client_secret (from that site’s ZITEOX_SITE_SECRET). Account verifies everything and returns session tokens; it also upserts core.app_connections (Connected apps).
  6. Finish — Site calls setSession, sets its own host-only cookie (sb-zx-admin-auth-token or sb-zx-client-auth-token), clears the handoff cookie, re-checks staff / two-step rules, then redirects to return_to.

Return-to rules

Handoff uses relative paths only. Site helpers (sanitizeAdminHandoffReturnTo / sanitizeClientHandoffReturnTo) call safeReturnTo with an empty origin allowlist and fallback /dashboard.

InputResult
Same-origin path like /projects?tab=1 or /dashboard/usersAccepted as-is
Empty / missing/dashboard
Full URL (https://…)Fallback /dashboard
Protocol-relative (//evil.example)Fallback /dashboard
Backslash or %5c tricksFallback /dashboard
javascript: / data: / other non-http(s) schemesFallback /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:

  1. Best-effort local sign-out on that site (scope: "local")
  2. Clear the handoff cookie
  3. 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

MethodBehaviour
POSTSign 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.
GET303 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).