Skip to main content

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 /projects

API Endpoints

GET/POST /api/auth/[…nextauth]

NextAuth.js dynamic route handler for all authentication operations. Supported operations:
  • GET /api/auth/signin - Sign in page
  • GET /api/auth/signout - Sign out
  • GET /api/auth/callback/google - OAuth callback
  • POST /api/auth/signin/google - Initiate Google OAuth
  • GET /api/auth/session - Get current session
  • GET /api/auth/csrf - Get CSRF token
  • GET /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:
Database:
  • 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

With analytics:

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

  1. Go to Google Cloud Console
  2. Create a new project or select existing
  3. Enable Google+ API
  4. Create OAuth 2.0 credentials
  5. Add authorized redirect URIs:
    • http://localhost:3000/api/auth/callback/google (development)
    • https://yourdomain.com/api/auth/callback/google (production)
  6. Copy Client ID and Client Secret to .env.local

Security Considerations

  • Never commit .env.local to version control
  • Use strong NEXTAUTH_SECRET (generate with openssl 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_URL matches your domain
  • No cookie-blocking extensions are active