Skip to main content

Initiate GitHub OAuth

GET /auth/github
Initiates the GitHub OAuth flow by redirecting the user to GitHub’s authorization page.

Behavior

  1. Generates a random CSRF state token (32 bytes, URL-safe)
  2. Stores the state in the database with a 10-minute expiration
  3. Cleans up expired OAuth states
  4. Redirects to GitHub OAuth with required scopes

OAuth Scopes

  • repo - Full control of private repositories
  • read:user - Read user profile data
  • user:email - Access user email addresses
  • read:org - Read organization membership

Response

redirect
RedirectResponse
Redirects to https://github.com/login/oauth/authorize with:
  • client_id - Application’s GitHub client ID
  • scope - Required OAuth scopes
  • state - CSRF protection token

GitHub OAuth Callback

GET /auth/github/callback?code=abc123&state=xyz789
Handles the OAuth callback from GitHub after user authorization.

Query Parameters

code
string
required
Authorization code from GitHub
state
string
required
CSRF state token that matches the one stored in the database

Behavior

  1. Validates the state token against the database
  2. Exchanges the authorization code for a GitHub access token
  3. Fetches the user’s GitHub profile
  4. Creates or updates the user in the database
  5. Encrypts and stores the GitHub access token
  6. Generates a JWT access token
  7. Sets the JWT as an httpOnly cookie
  8. Redirects to the frontend dashboard

Response

redirect
RedirectResponse
Redirects to {FRONTEND_URL}/dashboard with:
  • access_token cookie (httpOnly, secure in production)
  • Cookie max age: ACCESS_TOKEN_EXPIRE_MINUTES * 60 seconds
  • SameSite: none (production) or lax (development)

Error Responses

400
error
Invalid or expired OAuth state - The state token is missing, invalid, or expired
400
error
Failed to exchange GitHub code - GitHub token exchange failed
400
error
Failed to fetch GitHub user - Unable to retrieve GitHub user profile

Reconnect GitHub Account

GET /auth/github/reconnect
Cookie: access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Revokes the current GitHub OAuth token and initiates a fresh OAuth flow. Use this when the user needs to grant access to a new organization.

Authentication

access_token - JWT token in httpOnly cookie

Behavior

  1. Decrypts the stored GitHub access token
  2. Revokes the token with GitHub
  3. Deletes the JWT cookie
  4. Generates a new CSRF state token
  5. Redirects to GitHub OAuth authorization page

Response

redirect
RedirectResponse
Redirects to GitHub OAuth with:
  • Deleted access_token cookie
  • New OAuth flow initiated
401
error
Not authenticated - Missing or invalid JWT token
Token revocation failures are logged but don’t prevent the reconnection flow from continuing.

Build docs developers (and LLMs) love