# 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.

URL: https://www.ghosty.studio/en/docs/factory/oauth-in-previews

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
```

| Provider | Where |
|---|---|
| Google | [Credentials](https://console.cloud.google.com/apis/credentials) → your OAuth client → "Authorized redirect URIs" |
| GitHub | [OAuth Apps](https://github.com/settings/developers): create a separate one for previews (GitHub takes a single callback) |
| Shopify | Partners → your app → Configuration → "Allowed redirection URL(s)" (or `redirect_urls` in `shopify.app.toml`) |
| Microsoft | [Entra → App registrations](https://entra.microsoft.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade) → Authentication → Web → Redirect URIs |
| Facebook | [Meta for Developers](https://developers.facebook.com/apps/) → Facebook Login → Settings → "Valid OAuth Redirect URIs" |

## 2. Variables every preview already has

| Variable | What it is |
|---|---|
| `GS_PREVIEW_OAUTH_REDIRECT_URI` | The callback above. Use it as `redirect_uri`. |
| `GS_PREVIEW_OAUTH_STATE_KEY` | Key to sign the `state`. Only valid for that preview's host. |
| `PREVIEW_HOST` | The 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`.
