Software Factory

OAuth login in previews

One fixed Ghosty callback forwards the code from Google, GitHub, Shopify, Microsoft or Facebook to each PR preview, without registering a redirect URI per preview.

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)

https://www.ghosty.studio/preview-oauth/callback
ProviderWhere
Google → your OAuth client → "Authorized redirect URIs"
GitHub: create a separate one for previews (GitHub takes a single callback)
ShopifyPartners → your app → Configuration → "Allowed redirection URL(s)" (or redirect_urls in shopify.app.toml)
Microsoft → Authentication → Web → Redirect URIs
Facebook → Facebook Login → Settings → "Valid OAuth Redirect URIs"

2. Variables every preview already has

VariableWhat it is
GS_PREVIEW_OAUTH_REDIRECT_URIThe callback above. Use it as redirect_uri.
GS_PREVIEW_OAUTH_STATE_KEYKey to sign the state. Only valid for that preview's host.
PREVIEW_HOSTThe preview's host.

3. Sign the state

Copy this helper (no dependencies). Outside a preview it returns your state unchanged:

js
import { createHmac } from "node:crypto";
export function previewOAuthState(appState, callbackPath) {  const key = process.env.GS_PREVIEW_OAUTH_STATE_KEY, host = process.env.PREVIEW_HOST;  if (!key || !host) return appState;  const e = Math.floor(Date.now() / 1000) + 600;  const body = Buffer.from(JSON.stringify({ h: host, p: callbackPath, e })).toString("base64url");  return body + "." + createHmac("sha256", key).update(body).digest("base64url") + "~" + appState;}

When building the authorization URL:

js
const redirectUri = process.env.GS_PREVIEW_OAUTH_REDIRECT_URI ?? `${origin}/auth/google/callback`;const state = previewOAuthState(csrf, "/auth/google/callback");

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:

bash
ghosty factory env push <owner>/<repo> .env.preview --workspace <workspace># or: ghosty-dev env push .env.preview

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.