# Deploy the auth handler

> Create a GitHub App, host the sign-in handler from editlet/auth and point the editor at it.

Deploy the auth handler to turn on **Sign in with GitHub** on your live site. Without it, editors sign in with a
personal access token.

## Create a GitHub App

1. In GitHub, open **Settings → Developer settings → GitHub Apps → New GitHub App**.
2. Set **Callback URL** to `<handler URL>/callback`, for example `https://example.com/api/editlet/oauth/callback`.
3. Keep **Expire user authorization tokens** on. Turn **Webhook** off.
4. Give the repository permissions Contents read/write, Pull requests read/write and Deployments read.
5. Generate a client secret, install the app on the content repository and copy the **Client ID** (not the App ID).

## Host the handler

The handler is one function, `handleRequest(request, env)`, for any host with the Fetch API. Run it on one stable
public URL.

**Next.js:**

```ts
// app/api/editlet/oauth/[...path]/route.ts
import { handleRequest, type Env } from 'editlet/auth';

const handler = (request: Request) => handleRequest(request, process.env as Env);
export { handler as GET, handler as OPTIONS, handler as POST };
```

Keep this route out of middleware and site-wide CSP and COOP headers, or the sign-in popup breaks. An Astro SSR
endpoint works the same way: `export const ALL = ({ request }) => handleRequest(request, process.env)`.

**Cloudflare Workers** (any site, also a static one):

```ts
// worker.ts, with wrangler.toml: name = "editlet-auth", main = "worker.ts", keep_vars = true
import { handleRequest, type Env } from 'editlet/auth';

export default { fetch: (request: Request, env: Env) => handleRequest(request, env) };
```

Run `npx wrangler@4 deploy`, then `npx wrangler@4 secret put GITHUB_CLIENT_SECRET`. Set the other vars in the
dashboard. The URL is `https://editlet-auth.<account>.workers.dev`.

**Node, Bun, Deno or Docker:** export `{ fetch: (request) => handleRequest(request, process.env) }` from
`server.mjs`. Serve it with `npx srvx@1.0.8 serve --prod --entry server.mjs`, `bun run server.mjs` or
`deno serve --allow-env --allow-net server.mjs`. Put it behind HTTPS and point health checks at `/health` (`204`).

## Set the variables

Set `GITHUB_CLIENT_ID`, `GITHUB_CLIENT_SECRET` and `ALLOWED_ORIGINS`, plus `PUBLIC_URL` when the handler runs under a
path or behind a proxy ([Environment variables](https://editlet.gunkin.dev/docs/environment.md)).

`ALLOWED_ORIGINS` is the main security setting: any page on a listed origin can get a signed-in user's token. List
your site's origin, because the admin runs there. Never put a wildcard on a shared domain (`vercel.app`, `pages.dev`,
`github.io`, `workers.dev`) or list a shared CDN.

## Point the editor at the handler

```ts
// next.config.ts (the Astro integration takes the same admin option)
export default withEditlet(nextConfig, { admin: { authUrl: 'https://example.com/api/editlet/oauth' } });
```

Use the production URL, so preview deploys sign in there too. On Vercel, build it from
`VERCEL_PROJECT_PRODUCTION_URL`.

## Test locally

Add `http://localhost:8787/callback` as an extra callback URL on the app and run the handler on port 8787. Use Chrome
or Firefox: Safari can drop `Secure` cookies on `localhost`, which shows as `state_mismatch`.
