Overview
Better Hub uses session-based authentication powered by Better Auth. All authenticated API requests require a valid session cookie. There are two ways to authenticate:- GitHub OAuth - Standard OAuth flow (recommended for web apps)
- Personal Access Token (PAT) - Sign in with a GitHub PAT (useful for scripts and CLI tools)
Authentication Methods
GitHub OAuth (Recommended)
The standard authentication flow uses GitHub OAuth with configurable scopes.Initiate OAuth
Redirect users to the Better Auth OAuth endpoint:The OAuth flow will redirect to GitHub for authorization.
User Authorizes
Users authorize Better Hub to access their GitHub account with these default scopes:
read:user- Read user profile datauser:email- Access user email addressespublic_repo- Access public repositories
Users can grant additional scopes during sign-in for extended permissions (private repos, etc.)
Session Created
After successful authorization, Better Auth creates a session and sets a session cookie.The session is stored in the
session table in PostgreSQL:Unique session identifier
Unique session token
Associated user ID
Session expiration timestamp
IP address of the session
Browser user agent string
Personal Access Token (PAT)
For scripts, CLI tools, or testing, you can authenticate using a GitHub Personal Access Token.PAT authentication creates a session just like OAuth. The PAT is encrypted and stored in the
Account table.Create a GitHub PAT
- Go to GitHub Settings → Developer settings → Personal access tokens
- Generate a new token (classic) with these scopes:
read:useruser:emailpublic_repo(orrepofor private repositories)
Sign In with PAT
POST /api/auth/pat-signin
Request Body:
GitHub Personal Access Token
How PAT Authentication Works
- Better Hub validates the PAT against GitHub’s API
- Retrieves the authenticated user’s profile
- Creates or updates the user in the database
- Encrypts the PAT using AES symmetric encryption
- Stores the encrypted PAT in the
Accounttable - Creates a session and sets a session cookie
Session Management
Session Storage
Sessions are stored in two places:- Database - PostgreSQL
sessiontable for persistence - Cookie Cache - JWE-encrypted cookie for performance
- Name: Set by Better Auth (typically
better-auth.session_token) - Max Age: 7 days (604,800 seconds)
- Strategy: JWE (JSON Web Encryption)
- Secure: True in production
- HttpOnly: True (prevents XSS attacks)
- SameSite: Lax
Check Session Status
To verify if a session is active:Sign Out
To end a session:Making Authenticated Requests
Once authenticated, include the session cookie with every API request.Browser Requests
When making requests from a browser, cookies are included automatically:Server-Side Requests
For server-side or script-based requests, you need to manage cookies manually:Authorization Headers
Better Hub uses session cookies for authentication, notAuthorization headers. This is different from many REST APIs that use Bearer tokens.
Error Responses
Authentication errors return these status codes:401 Unauthorized
The request lacks valid authentication credentials.- No session cookie provided
- Session expired
- Invalid session token
403 Forbidden
The authenticated user lacks permission for the requested resource.- Insufficient GitHub scopes
- Access denied by GitHub
- User banned or restricted
GitHub Token Management
OAuth Token Storage
GitHub OAuth access tokens are:- Encrypted using AES symmetric encryption
- Stored in the
Accounttable’saccessTokenfield - Automatically refreshed when expired (if refresh token available)
Token Scopes
The user’s current GitHub scopes can be checked via:Account Updates
Better Hub is configured withupdateAccountOnSignIn: true, which means:
- Scopes are updated on each sign-in
- Access tokens are refreshed automatically
- Account information stays in sync with GitHub
Security Best Practices
Use HTTPS
Always use HTTPS in production to prevent session hijacking
Secure Cookies
Session cookies are HttpOnly and Secure in production
Rotate PATs
Regularly rotate Personal Access Tokens
Minimal Scopes
Only request GitHub scopes you actually need
Session Impersonation: Better Hub includes an admin impersonation feature (via the
admin plugin). Impersonated sessions have the impersonatedBy field set in the database.Next Steps
User Settings API
Manage user preferences and settings
GitHub Data API
Access repositories, issues, and PRs
AI Features
Use AI-powered GitHub assistance
Billing API
Check credits and usage limits