Overview
Study Sync uses Firebase Authentication for user identity management and MongoDB for storing user profiles and application data. This hybrid approach provides:- Secure authentication with industry-standard OAuth providers
- No custom password storage or management
- JWT-based stateless API authentication
- Automatic user provisioning in MongoDB
Architecture
Client-Side: Firebase Authentication SDK
Location:src/lib/firebase.js:1
The client uses Firebase JavaScript SDK to handle:
- Email/password sign-up and sign-in
- Google OAuth sign-in
- Password reset
- Session management
- ID token generation
Server-Side: Firebase Admin SDK
Location:src/lib/firebase-admin.js:1
The server uses Firebase Admin SDK to:
- Verify ID tokens from client requests
- Decode user information from tokens
- Ensure token authenticity and validity
MongoDB: User Profile Storage
Location:src/lib/db.js:74
MongoDB stores:
- User profile data (displayName, photoURL)
- Application-specific settings (notifications, role)
- Relationships (study plans, instances, progress)
Authentication Flow
1. User Registration
Client Implementation:src/providers/AuthProvider.jsx:28
src/lib/auth.js:17
2. User Login (Email/Password)
Client Implementation:src/app/(auth)/login/page.jsx:15
src/providers/AuthProvider.jsx:23
3. Google OAuth Login
Client Implementation:src/providers/AuthProvider.jsx:33
4. Auth State Monitoring
Location:src/providers/AuthProvider.jsx:80
The AuthProvider monitors Firebase auth state and automatically:
- Detects user login/logout
- Fetches ID tokens
- Enriches Firebase user with MongoDB profile
- Updates React context
5. API Request Authentication
Middleware Implementation:src/lib/auth.js:4
src/app/api/study-plans/route.js:154
6. Optional Authentication
Some routes (like public study plan browsing) support optional authentication to provide personalized features for logged-in users. Implementation:src/lib/auth.js:43
src/app/api/study-plans/route.js:24
Client-Side API Calls
API Helper Functions
Location:src/lib/api.js:17
Example: Create Study Plan
Location:src/lib/api.js:68
Security Considerations
Token Expiration
Firebase ID tokens expire after 1 hour. The Firebase SDK automatically refreshes tokens, and theAuthProvider fetches fresh tokens on each auth state change.
Token Storage
Tokens are stored in React state (not localStorage) to prevent XSS attacks. The Firebase SDK handles secure token storage internally.HTTPS Only
In production, all requests must use HTTPS to prevent token interception.Service Account Security
The Firebase service account key is stored as a base64-encoded environment variable to prevent accidental commits. Location:src/lib/firebase-admin.js:10
Role-Based Access Control
Users have arole field ("user" or "admin") stored in MongoDB. Protected routes can check this field:
Password Reset Flow
Client Implementation:src/providers/AuthProvider.jsx:75
Logout Flow
Client Implementation:src/providers/AuthProvider.jsx:50
Environment Variables
Client-Side (Public)
Server-Side (Private)
Testing Authentication
Manual Testing
- Sign Up: Create a new account via
/register - Check MongoDB: Verify user document created with
firebaseUid - Make API Request: Use browser DevTools to inspect
Authorizationheader - Check Token: Decode JWT at jwt.io to verify contents
Error Handling
Location:src/app/(auth)/login/page.jsx:26
Summary
Authentication Architecture:- Firebase handles identity and token generation
- MongoDB stores user profiles and app data
- JWT tokens authenticate API requests
- Stateless, scalable, and secure
src/lib/firebase.js- Client SDKsrc/lib/firebase-admin.js- Server SDKsrc/lib/auth.js- Authentication middlewaresrc/providers/AuthProvider.jsx- React context provider