Firebase Setup for Help Desk
This guide walks you through setting up Firebase services for the Help Desk application.
1. Create a Firebase Project
- Go to the Firebase Console
- Click “Add project”
- Enter a project name (e.g., “Help Desk”)
- Choose whether to enable Google Analytics (recommended)
- Accept the terms and create the project
2. Register Your Web App
- From the Firebase project dashboard, click the ”</>” icon (Web)
- Enter a nickname for your app (e.g., “Help Desk Web”)
- Optionally set up Firebase Hosting
- Click “Register app”
- Copy the Firebase configuration object for use in your
.env.localfile
3. Configure Authentication
- In the Firebase Console, go to “Authentication”
- Click “Get started”
- Enable the “Google” sign-in provider:
- Click “Google” in the list of providers
- Enable the provider
- Configure your support email
- Save
4. Set Up Firestore Database
- In the Firebase Console, go to “Firestore Database”
- Click “Create database”
- Start in production mode (recommended)
- Choose a location close to your users
- Deploy Firestore security rules:
Terminal window firebase deploy --only firestore:rules - Deploy Firestore indexes:
Terminal window firebase deploy --only firestore:indexes
5. Set Up Firebase Storage
- In the Firebase Console, go to “Storage”
- Click “Get started”
- Choose a location close to your users
- Deploy Storage security rules:
Terminal window firebase deploy --only storage:rules
6. Set Up Firebase Functions
- In the Firebase Console, go to “Functions”
- If prompted, upgrade to the Blaze (pay as you go) plan
- This is required for Functions, but the free tier is generous
- Initialize Firebase in your local project if not already done:
Terminal window firebase init - Deploy the Functions:
Terminal window firebase deploy --only functions
7. Set Up Service Account for Admin SDK
Option 1: Using a Service Account JSON File
- Go to your Firebase project in the Firebase Console
- Click on the gear icon (⚙️) next to “Project Overview” and select “Project settings”
- Go to the “Service accounts” tab
- Click “Generate new private key” button
- Save the JSON file securely (do not commit this to your repository!)
Now, you have two options for using this key:
A. Convert to environment variable (recommended for production)
For security reasons, it’s best to store the entire JSON content as an environment variable:
- Open the JSON file
- Copy ALL the contents including the curly braces
- In your
.env.localfile, add:
FIREBASE_SERVICE_ACCOUNT_KEY='{"type":"service_account","project_id":"your-project-id",...}'IMPORTANT: Make sure to enclose the entire JSON content in single quotes, and ensure it’s properly escaped if needed.
B. Reference a local file (development only)
Alternatively, for local development, you can reference the file directly:
// In src/lib/firebase/firebaseAdmin.ts (DEVELOPMENT ONLY - DO NOT USE IN PRODUCTION)import * as fs from 'fs';import { resolve } from 'path';
let serviceAccountKey;try { const serviceAccountPath = resolve('./service-account.json'); serviceAccountKey = JSON.parse(fs.readFileSync(serviceAccountPath, 'utf8'));} catch (error) { console.error('Error loading service account:', error); serviceAccountKey = {};}Option 2: Using Application Default Credentials
For simpler setup, particularly in environments like Firebase Hosting or Google Cloud:
- Set up the Firebase CLI and log in with
firebase login - Use the application default credentials in your code (already set up as fallback)
8. Configure Environment Variables
Create a .env.local file with your Firebase configuration:
# Firebase ConfigNEXT_PUBLIC_FIREBASE_API_KEY=your-api-keyNEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=your-project.firebaseapp.comNEXT_PUBLIC_FIREBASE_PROJECT_ID=your-project-idNEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=your-project.appspot.comNEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=your-sender-idNEXT_PUBLIC_FIREBASE_APP_ID=your-app-idNEXT_PUBLIC_FIREBASE_MEASUREMENT_ID=your-measurement-id
# Firebase AdminFIREBASE_SERVICE_ACCOUNT_KEY={"type":"service_account",...}9. Install Firebase CLI Tools
If you haven’t already, install the Firebase CLI:
npm install -g firebase-toolsLogin to your Firebase account:
firebase login10. Initialize Firebase in Your Project
If not already initialized:
firebase initSelect the following features:
- Firestore
- Functions
- Storage
- Hosting
- Emulators (optional, but useful for local development)
11. Deploy to Firebase
Deploy everything:
./scripts/deploy.shOr deploy specific features:
# Deploy only Functionsfirebase deploy --only functions
# Deploy only Hostingfirebase deploy --only hosting
# Deploy only Firestore rulesfirebase deploy --only firestore:rules
# Deploy only Storage rulesfirebase deploy --only storage:rules12. Checking Deployment Status
After deploying, you can check your deployment status:
- Firebase Hosting: Visit
https://YOUR-PROJECT-ID.web.app - Firebase Functions: Check the Firebase Console > Functions
- Firestore Rules: Check Firebase Console > Firestore > Rules
- Storage Rules: Check Firebase Console > Storage > Rules
Development Mode Without Firebase Admin
If you don’t have a Firebase service account set up yet, you can run in development mode:
- Set this environment variable:
SKIP_AUTH_VERIFICATION=trueThis will bypass authentication checks and provide mock data for users and other admin operations.
IMPORTANT: This should ONLY be used during development.
Troubleshooting
Authentication Issues
- Make sure your Firebase Auth domain is correctly set in
.env.local - Check that the Google Sign-In provider is enabled
- Verify allowed domains if you’ve restricted sign-ins
Function Deployment Issues
- Check for errors in the Firebase Console > Functions > Logs
- Make sure you’re on the Blaze plan (required for outbound network requests)
- Check Node.js version compatibility (should be Node.js 20)
Hosting Issues
- Verify your
firebase.jsonconfiguration - Check that the
publicdirectory path is correct - Make sure the
nextServerfunction exists and is exported correctly
Database Access Issues
- Check Firestore security rules
- Verify that user roles are being set correctly
- Check for database indexes if you’re using complex queries
”Expected property name or ’}’ in JSON at position 1”
This error occurs when the FIREBASE_SERVICE_ACCOUNT_KEY environment variable contains invalid JSON. Common causes:
- Missing quotes around the JSON content
- JSON content has unescaped quotes
- The environment variable is not set correctly
Solutions:
- Double-check that your JSON is properly formatted and enclosed in quotes
- Try escaping any special characters in your JSON string
- Ensure you’re not adding extra quotes inside the JSON content