GitHub OAuth
Workers Auth supports GitHub OAuth 2.0 as a first-class authentication strategy. Users click a link, authorize on GitHub, and are redirected back with a session cookie — no client-side token handling needed.
How it works
Section titled “How it works”1. User clicks "Sign in with GitHub" -> GET /auth/github2. Server generates state, stores in KV, redirects to GitHub3. User authorizes the app on GitHub4. GitHub redirects back -> GET /auth/github/callback?code=xxx&state=xxx5. Server validates state, exchanges code for access token6. Server fetches user's primary email from GitHub API7. Server creates user (if new), creates session8. Server sets session cookie and redirects to your app1. Create a GitHub OAuth App
Section titled “1. Create a GitHub OAuth App”- Go to GitHub Developer Settings > OAuth Apps
- Click New OAuth App
- Set Authorization callback URL to
https://your-app.workers.dev/auth/github/callback - Copy the Client ID and generate a Client Secret
2. Set secrets
Section titled “2. Set secrets”wrangler secret put GITHUB_CLIENT_IDwrangler secret put GITHUB_CLIENT_SECRET3. Configure the strategy
Section titled “3. Configure the strategy”import { GitHubStrategy } from 'workers-auth/authn/github';
GitHubStrategy({ clientId: process.env.GITHUB_CLIENT_ID!, clientSecret: process.env.GITHUB_CLIENT_SECRET!, scopes: ['user:email'], // default});Configuration
Section titled “Configuration”| Option | Type | Default | Description |
|---|---|---|---|
clientId | string | required | GitHub OAuth App Client ID |
clientSecret | string | required | GitHub OAuth App Client Secret |
scopes | string[] | ['user:email'] | OAuth scopes to request |
Routes registered
Section titled “Routes registered”| Route | Method | Description |
|---|---|---|
/auth/github | GET | Redirects to GitHub authorization page |
/auth/github/callback | GET | Handles the OAuth callback |
User matching
Section titled “User matching”Workers Auth fetches the user’s primary verified email from the GitHub API. If a user with that email already exists (e.g. from a magic link sign-in), the existing account is used. Otherwise, a new user is created.
This means users can sign in with either magic link or GitHub and are linked to the same account as long as they use the same email.
Frontend integration
Section titled “Frontend integration”<a href="/auth/github">Sign in with GitHub</a>The entire flow is server-side redirects. No JavaScript required.