Overview
GitHub OAuth enables:- User Authentication - Users sign in with their GitHub account
- Repository Access - Nectr can read PR data and post review comments
- Organization Access - Users can grant access to organization repositories
repo- Full control of private repositoriesread:user- Read user profile datauser:email- Access user email addressesread:org- Read organization membership
Creating a GitHub OAuth App
Navigate to GitHub Developer Settings
Go to github.com/settings/developers and click New OAuth App.Or navigate manually:
- GitHub → Settings
- Developer settings (bottom of sidebar)
- OAuth Apps → New OAuth App
Configure OAuth App Details
Fill in the application details:
| Field | Value |
|---|---|
| Application name | Nectr AI PR Review (or your preferred name) |
| Homepage URL | Your frontend URL (e.g., https://your-app.vercel.app) |
| Application description | Optional: “AI-powered pull request review agent” |
| Authorization callback URL | https://your-backend.up.railway.app/auth/github/callback |
OAuth Flow
Nectr implements a standard OAuth 2.0 authorization code flow with CSRF protection.Step-by-Step Process
1. User Initiates Login
1. User Initiates Login
Frontend redirects to
GET /auth/github2. Backend Generates CSRF State
2. Backend Generates CSRF State
Backend generates a random state token and stores it in the database with a 10-minute expiration.This protects against CSRF attacks by ensuring the callback comes from a legitimate request.
3. Redirect to GitHub Authorization
3. Redirect to GitHub Authorization
4. User Grants Access
4. User Grants Access
GitHub shows an authorization screen asking the user to grant access to:
- Their public and private repositories
- Their profile information
- Organization memberships (if any)
code- Temporary authorization codestate- The CSRF token we provided
5. Backend Validates and Exchanges Code
5. Backend Validates and Exchanges Code
6. Store User and Set JWT Cookie
6. Store User and Set JWT Cookie
Token Security
Encryption
GitHub OAuth tokens are encrypted before being stored in the database using theSECRET_KEY environment variable.
JWT Cookies
Nectr uses JWT tokens stored in httpOnly cookies for session management:| Property | Development | Production |
|---|---|---|
httponly | true | true |
secure | false | true |
samesite | lax | none |
max_age | 1440 minutes (24h) | 1440 minutes (24h) |
Granting Organization Access
By default, users can only access their personal repositories. To access organization repositories:User Clicks Reconnect
Navigate to
GET /auth/github/reconnect endpoint.This revokes the current OAuth token and redirects to a fresh GitHub authorization screen.GitHub Shows Org Access Screen
GitHub displays an authorization screen with organization access requests.Users can:
- Grant access to specific organizations
- Approve access for the OAuth app
Testing OAuth Locally
Local Development Setup
-
Create a separate OAuth App for local development
GitHub doesn’t allow
localhostin production OAuth apps, so create a dedicated dev app:- Homepage URL:
http://localhost:3000 - Callback URL:
http://localhost:8000/auth/github/callback
- Homepage URL:
-
Configure local environment
-
Test the flow
You should be redirected to GitHub, then back to your frontend dashboard after approval.
Troubleshooting
Redirect URI Mismatch Error
Redirect URI Mismatch Error
Error:
redirect_uri_mismatchCause: The callback URL in your GitHub OAuth App doesn’t match the URL Nectr is using.Solution:- Check your
BACKEND_URLin.env - Verify the callback URL in GitHub OAuth App settings matches:
{BACKEND_URL}/auth/github/callback - Ensure there are no trailing slashes or typos
Invalid OAuth State Error
Invalid OAuth State Error
Error:
Invalid or expired OAuth stateCause:- State token expired (>10 minutes)
- Database connection issue
- CSRF token mismatch
- Try the login flow again (state tokens expire after 10 minutes)
- Check database connectivity
- Ensure cookies are enabled in your browser
Session Expired / Token Decryption Failed
Session Expired / Token Decryption Failed
Error:
Session expired — please log out and sign in againCause: SECRET_KEY changed after the user authenticated.Solution:- User must log out and sign in again
- All encrypted tokens are invalidated when
SECRET_KEYchanges - Avoid changing
SECRET_KEYin production
Can't Access Organization Repositories
Can't Access Organization Repositories
Cause: User hasn’t granted organization access to the OAuth app.Solution:
- Navigate to
/auth/github/reconnectendpoint - This will revoke the current token and show a fresh GitHub authorization screen
- Grant access to the desired organizations
- Complete the OAuth flow again
API Endpoints
| Method | Endpoint | Description |
|---|---|---|
GET | /auth/github | Start OAuth flow |
GET | /auth/github/callback | OAuth callback (handles code exchange) |
GET | /auth/me | Get current authenticated user |
GET | /auth/github/reconnect | Revoke token and re-authorize (for org access) |
POST | /auth/logout | Clear auth cookie |
app/auth/router.py:18-203
Next Steps
Environment Variables
View all configuration options
Webhooks
Configure GitHub webhooks for PR events