Firebase Authentication Setup
This guide explains how to configure Firebase Authentication and integrate it with the backend.
Step 1 – Create a Firebase Project
- Go to https://console.firebase.google.com
- Click on "Add project"
- Enter a project name (e.g.
hudi-backend) - Disable Google Analytics (optional)
- Click "Create"
Step 2 – Enable Email/Password Sign-in
- In your Firebase project console, go to Authentication > Sign-in method
- Enable the Email/Password provider
- Save changes
Step 3 – Get Project ID and Public Keys
- Go to Project Settings > General
- Copy the Project ID (you’ll need it in
.env) - Visit the following URL to access Firebase's public keys (used to verify tokens):
https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com
These are already handled in firebase_public_keys.dart.
Step 4 – Set Up Environment Variables
In your .env or .env.local file, add:
FIREBASE_PROJECT_ID=your_project_id
How It Works
The backend uses Firebase ID tokens sent in the Authorization: Bearer <token> header.
The checkAuth() middleware:
- Verifies the token signature
- Decodes the user ID (uid)
- Loads the corresponding user from MongoDB
Token Validation Route
You can test token verification using the following route:
GET /validateToken
Pass a valid Firebase token in the Authorization header to check if authentication works properly.
Last updated: 2025-04-01