Overview
The LSAT Training Platform uses NextAuth.js for authentication, integrated with Firebase for user management and data storage. The authentication system supports multiple providers and uses JWT-based sessions.Authentication Flow
The platform implements a hybrid authentication approach:- NextAuth.js handles the authentication logic and session management
- Firebase Authentication manages credential validation
- Firestore stores user profile data
- JWT tokens maintain session state
Supported Authentication Methods
1. Google OAuth
Users can sign in using their Google account through OAuth 2.0.2. Email/Password Credentials
Traditional email and password authentication backed by Firebase.Session Management
JWT Strategy
The platform uses JWT (JSON Web Token) strategy for session management:- Token Storage: HTTP-only cookies (secure)
- Token Lifetime: Configurable via NextAuth
- User Data: Fetched from Firestore on each session validation
Session Object Structure
Accessing Current Session
Firebase Integration
Client-Side Firebase
The platform initializes Firebase on the client for authentication operations:Server-Side Firebase Admin
Firebase Admin SDK is used for server-side operations:Required Environment Variables
Firebase project API key (client-side)
Firebase authentication domain (client-side)
Firebase project ID (client-side)
Firebase storage bucket (client-side)
Firebase messaging sender ID (client-side)
Firebase app ID (client-side)
Firebase Analytics measurement ID (client-side, optional)
Firebase service account email (server-side)
Firebase service account private key (server-side)
Secret key for NextAuth JWT encryption
Google OAuth client ID
Google OAuth client secret
User Data Storage
Firestore User Document
When a user authenticates, their data is stored in Firestore:Data Synchronization
- On Sign-In: User data is created/updated in Firestore
- On Session Load: User data is fetched from Firestore and attached to session
- On Profile Update: Changes are persisted to Firestore
Security Best Practices
JWT Secret Management
JWT Secret Management
The Never commit this secret to version control.
NEXTAUTH_SECRET should be a strong, randomly generated string:Firebase Private Key
Firebase Private Key
The Store the key with literal
FIREBASE_PRIVATE_KEY contains newline characters that must be properly escaped:\n strings in your .env file.Client vs Server Environment Variables
Client vs Server Environment Variables
- NEXT_PUBLIC_: Exposed to the browser, use for client-side Firebase
- No prefix: Server-only, use for sensitive credentials
FIREBASE_PRIVATE_KEY with NEXT_PUBLIC_.Session Validation
Session Validation
Always validate sessions on protected routes:
Error Handling
Common Authentication Errors
Returned when credentials don’t match any Firebase user or the password is incorrect.
Returned when the sign-in request is missing required credentials.
Occurs when required environment variables are missing or invalid.Check that all required Firebase and OAuth configuration is set.
Error Handling Example
Sign Out
To sign out a user and clear their session:Next Steps
NextAuth API
Detailed NextAuth endpoint documentation
User Profile
Managing user profiles and data
User Onboarding
Complete user onboarding workflow
Update User Data
Update user information and preferences