Overview
Firebase is configured in two parts:- Client SDK (
src/lib/firebase.js) - Handles user authentication in the browser - Admin SDK (
src/lib/firebase-admin.js) - Server-side authentication and user management
Prerequisites
- Google/Firebase account
- Firebase project created in Firebase Console
Client-side Setup
Step 1: Create Firebase Project
Create new project
Go to Firebase Console and click “Add project”.
Enable Authentication
Navigate to Authentication > Sign-in method and enable your desired providers:
- Email/Password
- GitHub
- Other providers as needed
Step 2: Get Configuration Values
In Firebase Console, go to Project Settings > General > Your apps section. Copy the Firebase configuration object values:Step 3: Configure Environment Variables
Add these to your.env.local file:
Client Configuration Code
The Firebase client is configured insrc/lib/firebase.js:
Server-side Setup (Admin SDK)
The Firebase Admin SDK allows server-side operations like verifying ID tokens and managing users.Step 1: Generate Service Account Key
Step 2: Encode Service Account Key
The service account key must be base64-encoded to store as an environment variable:- Linux/Mac
- Windows PowerShell
- Node.js
Step 3: Set Environment Variable
Add the base64-encoded key to.env.local:
Admin Configuration Code
The Firebase Admin SDK is configured insrc/lib/firebase-admin.js:
Authentication Methods
Study Sync supports multiple authentication providers:Email/Password
- Enable in Firebase Console: Authentication > Sign-in method > Email/Password
- Toggle “Enable” and save
Google Sign-In
- Enable in Firebase Console: Authentication > Sign-in method > Google
- Toggle “Enable”
- Set project support email
- Save changes
GitHub Sign-In
- Create OAuth App in GitHub Settings
- Get Client ID and Client Secret
- Enable GitHub in Firebase Console
- Add Client ID and Secret
- Copy Authorization callback URL from Firebase and add to GitHub OAuth app
Security Rules
Study Sync uses Firebase Authentication tokens for API route protection. Tokens are verified server-side using the Admin SDK.Token Verification Example
Testing Configuration
Verify Client Setup
- Start your development server
- Open browser console
- Check for Firebase configuration errors
- If configured correctly, you should see no errors
Verify Admin Setup
- Check server logs when starting the application
- Look for:
✅ Firebase Admin SDK initialized - If you see errors, verify your base64-encoded key is correct
Troubleshooting
”Missing Firebase configuration variables”
Cause: One or moreNEXT_PUBLIC_FIREBASE_* variables are missing or empty.
Solution: Verify all six client variables are set in .env.local and restart your dev server.
”Failed to initialize Firebase Admin SDK”
Cause: Invalid or incorrectly encoded service account key. Solutions:- Verify the base64 encoding is correct (no line breaks)
- Ensure the service account JSON is valid
- Check that the key hasn’t been revoked in Firebase Console
”Firebase App already initialized”
Cause: Hot reload in development can cause multiple initialization attempts. Solution: The code already handles this withgetApps().length === 0 check. If you still see issues, restart your dev server.
Authentication not working
Check:- Sign-in method is enabled in Firebase Console
- Authorized domains include your deployment domain (Project Settings > Authorized domains)
- Browser console for specific error messages
Production Deployment
Environment Variables
Set all Firebase environment variables in your hosting platform:- Vercel: Project Settings > Environment Variables
- Netlify: Site Settings > Environment Variables
- Other platforms: Refer to platform documentation
Authorized Domains
Add your production domain to Firebase authorized domains:- Go to Authentication > Settings > Authorized domains
- Click “Add domain”
- Enter your production domain (e.g.,
yourdomain.com)
Security Best Practices
- Rotate service account keys regularly (every 90 days recommended)
- Use environment-specific projects (dev, staging, production)
- Enable App Check for additional security
- Monitor authentication logs for suspicious activity
- Set up billing alerts to prevent quota abuse
- Implement rate limiting on sensitive operations