Overview
Catafract uses NextAuth.js v4 for authentication with Google as the OAuth provider.Authentication Flow
1
User initiates sign-in
User navigates to
/login and clicks “Sign in with Google”2
OAuth redirect
NextAuth redirects to Google OAuth consent screen
3
User authorizes
User grants permissions to the application
4
Callback processing
Google redirects back to
/api/auth/callback/googleNextAuth processes the callback and:- Checks if user exists in database
- Creates new user if first-time sign-in
- Establishes session
5
Session established
Session cookie is set and user is redirected to
/projectsAPI Endpoints
GET/POST /api/auth/[…nextauth]
NextAuth.js dynamic route handler for all authentication operations. Supported operations:GET /api/auth/signin- Sign in pageGET /api/auth/signout- Sign outGET /api/auth/callback/google- OAuth callbackPOST /api/auth/signin/google- Initiate Google OAuthGET /api/auth/session- Get current sessionGET /api/auth/csrf- Get CSRF tokenGET /api/auth/providers- List available providers
Configuration
Environment Variables
AuthOptions
User Creation
When a user signs in for the first time, a new user record is created in Azure Cosmos DB:- Container:
users - Partition Key:
email
Session Management
Getting Current Session (Client)
Getting Current Session (Server)
Sign In
Client-Side Sign In
Login Page Example
Sign Out
Client-Side Sign Out
Protected Routes
Client-Side Protection
Server-Side Protection
API Route Protection
Session Configuration
Sessions are managed via cookies (default NextAuth.js behavior):- Cookie name:
next-auth.session-token - Cookie security: HttpOnly, Secure (in production)
- Session strategy: JWT (default)
- Session max age: 30 days (NextAuth default)
Provider Setup
Google OAuth Console
- Go to Google Cloud Console
- Create a new project or select existing
- Enable Google+ API
- Create OAuth 2.0 credentials
- Add authorized redirect URIs:
http://localhost:3000/api/auth/callback/google(development)https://yourdomain.com/api/auth/callback/google(production)
- Copy Client ID and Client Secret to
.env.local
Security Considerations
- Never commit
.env.localto version control - Use strong
NEXTAUTH_SECRET(generate withopenssl rand -base64 32) - Configure authorized redirect URIs carefully
- Enable 2FA on your Google Cloud account
- Monitor OAuth usage in Google Console
- Implement rate limiting for production
Troubleshooting
”Configuration Error”
Check that all environment variables are set:“Callback URL Mismatch”
Ensure the redirect URI in Google Console matches exactly:Session Not Persisting
Check that:- Cookies are enabled in browser
NEXTAUTH_URLmatches your domain- No cookie-blocking extensions are active