GitHub OAuth Setup
Create a GitHub OAuth App
Navigate to GitHub Developer Settings
Go to GitHub Settings > Developer Settings > OAuth Apps and click New OAuth App.
Configure the OAuth App
Fill in the application details:
- Application name:
Better Hub (Development)or your app name - Homepage URL:
http://localhost:3000(development) or your production URL - Authorization callback URL:
http://localhost:3000/api/auth/callback/github
GitHub Scopes
Better Hub requests minimal permissions by default and allows users to grant additional scopes as needed: Default scopes (required on sign-in):read:user- Read user profile informationuser:email- Access user email addressespublic_repo- Access public repositories
repo- Full repository access (for private repos)workflow- GitHub Actions workflow permissions
apps/web/src/lib/auth.ts:108.
Better Auth Configuration
Generate Auth Secret
Better Auth requires a secret for encrypting sessions and tokens:.env file:
Application URLs
Set the base URL for your application:Auth Configuration Details
Better Hub’s authentication is configured inapps/web/src/lib/auth.ts. Key features:
Database Adapter
Better Auth uses Prisma with PostgreSQL:Plugins Enabled
Activity tracking and analytics dashboard
Security monitoring and rate limiting
Admin user management and impersonation
Payment and subscription management (optional, enabled when Stripe is configured)
OAuth proxy for Vercel preview deployments
Custom plugin for GitHub Personal Access Token authentication
Session Configuration
Sessions are cached in encrypted cookies for performance:OAuth Token Encryption
GitHub OAuth tokens are encrypted before storage:Trusted Origins
CORS is configured for these origins:https://www.better-hub.com(production)https://better-hub-*-better-auth.vercel.app(Vercel previews)https://beta.better-hub.com(beta environment)
apps/web/src/lib/auth.ts:123.
User Model
Better Auth extends the base user model with custom fields:GitHub Personal Access Token (optional, for additional API access)
Tracks whether user has completed onboarding flow
apps/web/prisma/schema.prisma:11-39.
Session Management
Server-Side Session Access
UsegetServerSession() to access the session in server components and API routes:
Session Data Structure
The session object includes:GitHub API Integration
The session automatically includes GitHub user data and access token, cached in Redis for 1 hour:apps/web/src/lib/auth.ts:18-28.
Multi-Environment Setup
Development
Create development OAuth App
Create a GitHub OAuth App with callback URL:
http://localhost:3000/api/auth/callback/githubStaging/Preview
For Vercel preview deployments, use the OAuth proxy:Production
Create production OAuth App
Create a separate GitHub OAuth App with your production domain callback URL
Set production environment variables
Configure variables in your hosting platform (Vercel, Railway, etc.)
Security Best Practices
IP Address Tracking
Better Auth tracks IP addresses for security:Rate Limiting
The Sentinel plugin provides automatic rate limiting on authentication endpoints.Troubleshooting
OAuth Callback URL Mismatch
Error:redirect_uri_mismatch
Solution: Ensure the callback URL in your GitHub OAuth App exactly matches:
Invalid Client Error
Error:invalid_client
Solution: Verify GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET are correct and from the same OAuth App.
Session Not Persisting
Symptoms: Users logged out on page refresh Solutions:- Verify
BETTER_AUTH_SECRETis set and at least 32 characters - Check browser cookie settings (must allow cookies)
- Ensure
BETTER_AUTH_URLmatches the actual application URL
GitHub API Rate Limiting
Symptoms:getServerSession() returns partial data
Solution: Redis caching helps, but GitHub has rate limits. Consider:
- Using a GitHub Personal Access Token for higher limits
- Implementing additional caching layers
- Requesting only necessary data
Advanced Configuration
Custom Profile Mapping
Customize how GitHub profile data maps to user records:apps/web/src/lib/auth.ts:109-113.
Enable User Deletion
User self-service account deletion is enabled:Stripe Integration
When Stripe environment variables are configured, the Stripe plugin automatically:- Creates Stripe customers on signup
- Grants signup credits
- Manages subscriptions