Better Auth Integration
Better Auth is a modern authentication library that provides:- Multiple OAuth provider support
- Session management
- Rate limiting
- Secure cookie handling
- Database integration via Drizzle ORM
Authentication Configuration
The main authentication configuration is inlib/auth.ts:
Supported Providers
Scira supports three OAuth providers out of the box:GitHub
Sign in with GitHub account
Sign in with Google account
Twitter/X
Sign in with Twitter/X account
OAuth Setup Instructions
GitHub OAuth
Create a GitHub OAuth App
- Go to GitHub Developer Settings
- Click “New OAuth App”
- Fill in the application details:
- Application name:
Scira(or your app name) - Homepage URL:
https://yourdomain.com - Authorization callback URL:
https://yourdomain.com/api/auth/callback/github
- Application name:
- Click “Register application”
Get Client ID and Secret
After creating the app:
- Copy the Client ID
- Click “Generate a new client secret”
- Copy the Client Secret (it won’t be shown again)
Google OAuth
Create a Google Cloud Project
- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable the Google+ API for your project
Create OAuth 2.0 Credentials
- Navigate to APIs & Services → Credentials
- Click “Create Credentials” → “OAuth client ID”
- Configure the consent screen if prompted
- Select “Web application” as the application type
- Add authorized redirect URIs:
https://yourdomain.com/api/auth/callback/google- For local development:
http://localhost:3000/api/auth/callback/google
- Click “Create”
Twitter/X OAuth
Create a Twitter App
- Go to Twitter Developer Portal
- Create a new project or select an existing one
- Create a new app within the project
Configure OAuth 2.0
- Navigate to your app’s settings
- Enable OAuth 2.0
- Set the Type of App to “Web App”
- Add Callback URLs:
https://yourdomain.com/api/auth/callback/twitter- For local development:
http://localhost:3000/api/auth/callback/twitter
- Add Website URL:
https://yourdomain.com
Get Client ID and Secret
From the app settings:
- Copy the Client ID (OAuth 2.0)
- Copy the Client Secret (OAuth 2.0)
Session Management
Better Auth handles session management automatically with secure, HTTP-only cookies.Session Schema
Fromlib/db/schema.ts:
Session Features
- Automatic expiration - Sessions expire based on
expiresAttimestamp - Token-based - Unique token for each session
- IP tracking - Stores IP address for security monitoring
- User agent tracking - Records browser/device information
- Cascade deletion - Sessions are deleted when user is deleted
User Schema
User accounts are stored in theuser table:
Account Schema
OAuth provider accounts are linked to users via theaccount table:
Account Features
- Multi-provider support - Users can link multiple OAuth providers
- Token management - Stores access and refresh tokens
- Token expiration - Tracks token expiration times
- Scope tracking - Records OAuth scopes granted
Rate Limiting
Better Auth includes built-in rate limiting:Trusted Origins
Configure trusted origins for CORS and cookie security:Security Considerations
Use HTTPS
Always use HTTPS in production for secure cookie transmission
Secure secrets
Keep
BETTER_AUTH_SECRET and OAuth secrets secureValidate redirects
Only allow redirects to trusted origins
Monitor sessions
Track IP addresses and user agents for anomaly detection
Environment Variables
Ensure you have configured the authentication secret:Testing Authentication
Local Development
- Ensure OAuth apps have
http://localhost:3000/api/auth/callback/{provider}in their callback URLs - Configure environment variables in
.env.local - Start the development server:
- Navigate to
http://localhost:3000and test sign-in
Production
- Update OAuth app callback URLs to your production domain
- Configure production environment variables
- Verify
trustedOriginsincludes your production domain - Test authentication flow in production
Troubleshooting
OAuth redirect URI mismatch
OAuth redirect URI mismatch
Ensure the callback URL in your OAuth app settings exactly matches:Common issues:
- Missing trailing slash
- HTTP vs HTTPS mismatch
- Wrong subdomain (www vs non-www)
Session not persisting
Session not persisting
Check:
BETTER_AUTH_SECRETis set- Cookies are enabled in browser
- Domain is in
trustedOrigins - HTTPS is enabled in production
Database connection errors
Database connection errors
Verify:
DATABASE_URLis correct- Database migrations have been run
- User, session, and account tables exist
Rate limit errors
Rate limit errors
If you’re hitting rate limits during testing:
- Increase
maxin rate limit configuration - Clear Redis cache
- Wait for the rate limit window to expire
