OAuth login in previews
Updated 2026-11-22
Google, GitHub and the other providers only accept redirect URIs registered one by one, and every preview has its own host (sb-<box>-<port>.preview.ghosty.page). To make "Sign in with Google" work in any preview, register Ghosty's fixed callback once and it forwards the response to the preview that asked for it. It is Auth.js's AUTH_REDIRECT_PROXY_URL pattern. Ghosty stores no tokens or codes.
If your app uses OAuth, the Preview card (web, app, Teams) and ghosty-dev doctor tell you: the URL to copy, a link to the provider console, three steps and a Do it with Ghosty button that asks your agent to add it with your browser and show you the change before saving. Your agent never reads or copies secrets.
1. Register the callback (once)
2. Variables every preview already has
3. Sign the state
Copy this helper (no dependencies). Outside a preview it returns your state unchanged:
When building the authorization URL:
Ghosty checks the signature, that it hasn't expired (15 min max) and that the preview is still alive, then redirects to https://<preview>/auth/google/callback?code=…&state=<your original state> with everything else the provider sent. Your CSRF check doesn't change. If the provider uses response_mode=form_post, the callback accepts POST too.
When exchanging the code, send the same redirect_uri (Ghosty's), or the provider answers redirect_uri_mismatch.
Shopify signs the whole query with hmac, so the state arrives wrapped: verify the hmac first, then unwrap it with state.slice(state.indexOf("~") + 1).
4. Upload the provider keys
The keys (GOOGLE_CLIENT_ID, the secret…) go into the preview environment from your terminal, never in chat or the PR:
It adds without removing the others and rebuilds the previews. If any are missing, the card names them.
For tools
GET https://www.ghosty.studio/api/v2/preview-oauth/providers (public) returns the provider catalog (how each is detected in code, typical variable names, console and steps), the state format and the helper. A PR's preview includes oauth with what's missing: GET /api/v2/me/repos/{owner}/{name}/prs/{pr}/preview.