Add a new site or app
Checklist for wiring a new Ziteox site into Ziteox Account sign-in.
Add a new site or app
How to connect a new first-party site or tool to Ziteox Account (auth.ziteox.com), the same way Admin and Client do it.
Copy from ziteox-client for a normal product site (no staff / forced two-step). Copy from ziteox-admin only when the site needs staff and fresh two-step rules like Admin.
Checklist
1. Pick a client id
- Lowercase string, stable forever (e.g.
partner,rail). - Becomes
core.apps.keyand the handoffclient_id. - Account looks up the site secret as
ZITEOX_SITE_SECRET_<CLIENT_ID>(uppercased) viasiteSecretEnvKeyinpackages/auth— e.g.partner→ZITEOX_SITE_SECRET_PARTNER.
2. Register the app in core.apps
Add a migration under ziteox-admin/supabase/migrations/ that inserts (or updates) the row. Secrets stay in env, never in SQL.
insert into core.apps (
key,
label,
allowed_origins,
callback_urls,
require_staff,
require_second_factor,
max_second_factor_age_seconds
) values (
'partner',
'Ziteox Partner',
array['https://partner.ziteox.com']::text[],
array['https://partner.ziteox.com/auth/handoff']::text[],
false, -- staff gate
false, -- require two-step at handoff
null -- or e.g. 43200 for 12h freshness
)
on conflict (key) do update set
allowed_origins = excluded.allowed_origins,
callback_urls = excluded.callback_urls,
require_staff = excluded.require_staff,
require_second_factor = excluded.require_second_factor,
max_second_factor_age_seconds = excluded.max_second_factor_age_seconds;| Column | Typical product site | Admin-like site |
|---|---|---|
callback_urls | https://<host>/auth/handoff | same pattern |
require_staff | false | true |
require_second_factor | false | true |
max_second_factor_age_seconds | null | 43200 (12h) |
See Site registration and secrets for how Account reads these columns.
3. Generate and set the site secret
- Generate a strong random value (32+ random bytes, hex or base64).
- On Account (Cloudflare Worker): set
ZITEOX_SITE_SECRET_<APP>(uppercase client id). - On the new site: set
ZITEOX_SITE_SECRETto the same value. - Never commit, log, or paste the value into docs or tickets.
4. Add site routes and helpers
Copy and rename from ziteox-client (simplest template):
| Copy from | Role |
|---|---|
ziteox-client/src/app/auth/start/route.ts | Start handoff: state + PKCE verifier cookie → redirect to Account /handoff |
ziteox-client/src/app/auth/handoff/route.ts | Callback: check state, exchange code, setSession, redirect to return_to |
ziteox-client/src/app/auth/signout/route.ts | POST local sign-out; GET only redirects (no sign-out) |
ziteox-client/src/lib/auth-handoff.ts | Cookie helpers, build*HandoffStartUrl, redirect URI, return_to sanitize |
ziteox-client/src/lib/client-middleware-auth.ts | Session-optional paths (/sign-in, /auth/*) and redirect decisions |
ziteox-client/src/lib/ziteox-auth/session-cookie.ts | Host-only session cookie name + secure flag |
ziteox-client/src/app/sign-in/page.tsx | “Sign in with Ziteox Account” → /auth/start?return_to=… |
Wire middleware the same way: middleware.ts → updateSession, and teach session refresh to use your cookie options. Keep /auth/start, /auth/handoff, and /auth/signout reachable without a session.
Replace every hardcoded "client" / client origin helper with your client id and site origin helper from @ziteox/auth.
Start route skeleton (matches client/admin):
// app/auth/start/route.ts
import { NextResponse, type NextRequest } from "next/server";
import { getAuthAppOrigin } from "@ziteox/auth";
import { generateState, generateVerifier } from "@ziteox/auth/handoff";
// import your HANDOFF_COOKIE, cookie write opts, build*StartUrl, sanitizeReturnTo
export async function GET(request: NextRequest) {
const returnTo = sanitizeReturnTo(
request.nextUrl.searchParams.get("return_to") ??
request.nextUrl.searchParams.get("returnTo")
);
const state = generateState();
const verifier = generateVerifier();
const location = await buildHandoffStartUrl({
authOrigin: getAuthAppOrigin(),
state,
verifier,
returnTo,
});
const response = NextResponse.redirect(location, 303);
response.cookies.set(
HANDOFF_COOKIE,
JSON.stringify({ state, verifier, returnTo }),
handoffCookieWriteOptions()
);
return response;
}Sign-out route skeleton (POST only signs out; GET must not):
// app/auth/signout/route.ts
export async function GET(request: NextRequest) {
return NextResponse.redirect(new URL("/sign-in", request.url), 303);
}
export async function POST(request: NextRequest) {
const response = NextResponse.redirect(new URL("/sign-in", request.url), 303);
// createClientForResponse → signOut({ scope: "local" })
// clear handoff cookie
return response;
}Handoff callback is longer (token exchange, draft-user checks, optional org cookie). Copy the client file and change client_id, secret env, and redirect URI helpers — do not invent a shorter exchange.
5. Pick a unique session cookie name
Host-only cookie, no domain attribute. Pattern used today:
| Site | Cookie name |
|---|---|
| Admin | sb-zx-admin-auth-token |
| Client | sb-zx-client-auth-token |
| New site | sb-zx-<app>-auth-token |
Distinct names matter on localhost, where several apps can share a host and would otherwise fight over one Supabase session cookie.
6. Sign-in and sign-out UI
- Sign in: link or button to
/auth/start?return_to=<current path>(path only, same origin). - Sign out: HTML form
method="post"action="/auth/signout". Never a prefetched GET link to a logout URL.
7. Local development fixture
For local Account with ZITEOX_SITE_REGISTRY=local:
- Add the site to
localHandoffSitesFromEnv()inpackages/auth/src/handoff/registry.ts(today onlyadminandclientare built fromADMIN_APP_URL/CLIENT_APP_URL). - Prefer a site origin helper in
packages/auth/src/sites.ts(Partner already hasgetPartnerSiteOrigin). - Add a localhost /
*.localhostorigin toDEV_ORIGINSinsafe-return-to.tsif needed. - Give the site a fixed local port (Admin
3000, Client3001, Account3002) and matching*_APP_URL/AUTH_APP_URLnames. - Use matching dummy
ZITEOX_SITE_SECRET_<APP>(Account) andZITEOX_SITE_SECRET(site).
Details: Security rules and local development.
8. Account-side code that still lists sites
Secret lookup is generic (ZITEOX_SITE_SECRET_${clientId.toUpperCase()}). You still need to touch explicit lists / labels when adding an app:
| Location | What to update |
|---|---|
packages/auth/src/handoff/registry.ts | HandoffClientId, productionHandoffSites(), localHandoffSitesFromEnv() |
packages/auth/src/sites.ts | Origin helper + env names for the new host |
packages/auth/src/safe-return-to.ts | Prod / dev origin allowlists when used |
packages/auth/src/audit.ts | ZiteoxPortal if you record portal-specific auth events |
ziteox-auth/src/app/handoff/actions.ts | portalForClient, appLabel; org binding today runs only when clientId === "client" |
ziteox-auth/src/lib/app-connections.ts | Optional display-name override (else uses core.apps.label / key) |
ziteox-auth/src/app/handoff/page.tsx | Metadata / UI copy that assumes Admin vs Client |
Production registration itself comes from core.apps via SupabaseSiteRegistry — the migration is the source of truth there. The lists above matter for types, local fixtures, labels, and special handoff behavior.
9. Test checklist
- Sign in from a deep page; after handoff you land on that path
- Wrong
state→ failure page; no session - Expired or reused code → failure; code is single-use (~60s TTL)
- Sign out of the new site (
scope: "local"); Admin / Client / Account stay signed in - Staff / two-step: if
require_stafforrequire_second_factoris set, wrong account or missing / stale 2FA is rejected - Unregistered
redirect_urior wrong site secret → no tokens
10. Deploy order
- Apply the DB migration (
core.appsrow) - Set secrets on Account and on the new site (
ZITEOX_SITE_SECRET_<APP>+ZITEOX_SITE_SECRET) - Redeploy Account if you changed Account code or need new Worker secrets picked up
- Deploy the new site