Cross-Project Firebase Authentication Pattern
Overviewβ
toto-app and toto-app-stg each have their own independent Firebase Authentication. toto-bo uses cross-authentication with the respective toto-app environment:
toto-bo(production) authenticates againsttoto-app(toto-f9d2f)toto-bo-stg(staging) authenticates againsttoto-app-stg(toto-f9d2f-stg)
This allows the backoffice to use the same user accounts as the main app while maintaining environment isolation.
Core Principleβ
β
toto-app: Uses its own auth (toto-f9d2f)
β
toto-app-stg: Uses its own auth (toto-f9d2f-stg)
β
toto-bo: Cross-auth with toto-app (toto-f9d2f)
β
toto-bo-stg: Cross-auth with toto-app-stg (toto-f9d2f-stg)
Implementationβ
Environment Variablesβ
toto-appβ
| Variable | Production | Staging | Purpose |
|---|---|---|---|
FIREBASE_PROJECT_ID | toto-f9d2f | toto-f9d2f-stg | Auth and data storage |
toto-boβ
| Variable | Production | Staging | Purpose |
|---|---|---|---|
FIREBASE_AUTH_PROJECT_ID | toto-f9d2f | toto-f9d2f-stg | Auth token verification (cross-auth) |
FIREBASE_PROJECT_ID | toto-f9d2f | toto-f9d2f-stg | Data storage |
Why This Pattern?β
- Environment Isolation: Each environment (production/staging) has its own auth system
- Cross-Project Authentication: Backoffice can authenticate users from the main app
- User Account Consistency: Staging backoffice uses staging app accounts, production uses production accounts
- Data Isolation: Staging data remains separate from production
Implementation Detailsβ
toto-app Configurationβ
Production (apphosting.production.yaml):
env:
- variable: FIREBASE_PROJECT_ID
value: toto-f9d2f
Staging (apphosting.staging.yaml):
env:
- variable: FIREBASE_PROJECT_ID
value: toto-f9d2f-stg
toto-bo Configurationβ
Production (apphosting.production.yaml):
env:
- variable: FIREBASE_AUTH_PROJECT_ID
value: toto-f9d2f # Cross-auth with toto-app
- variable: FIREBASE_PROJECT_ID
value: toto-f9d2f
- variable: NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN
value: toto-f9d2f.firebaseapp.com
- variable: NEXT_PUBLIC_FIREBASE_PROJECT_ID
value: toto-f9d2f
Staging (apphosting.staging.yaml):
env:
- variable: FIREBASE_AUTH_PROJECT_ID
value: toto-f9d2f-stg # Cross-auth with toto-app-stg
- variable: FIREBASE_PROJECT_ID
value: toto-f9d2f-stg
- variable: NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN
value: toto-f9d2f-stg.firebaseapp.com
- variable: NEXT_PUBLIC_FIREBASE_PROJECT_ID
value: toto-f9d2f-stg
Code Implementationβ
toto-appβ
Uses its own Firebase project for both auth and data:
// Production: toto-f9d2f
// Staging: toto-f9d2f-stg
const app = initializeApp({
projectId: process.env.FIREBASE_PROJECT_ID,
});
const auth = getAuth(app);
const db = getFirestore(app);
toto-boβ
Uses cross-authentication with the respective toto-app environment:
// Initialize with data project
const app = initializeApp({
projectId: process.env.FIREBASE_PROJECT_ID, // toto-f9d2f or toto-f9d2f-stg
});
// Initialize with auth project (cross-auth with toto-app)
const authProjectId = process.env.FIREBASE_AUTH_PROJECT_ID; // toto-f9d2f or toto-f9d2f-stg
const authApp = initializeApp({
projectId: authProjectId,
}, 'auth');
// Get auth instance from toto-app (cross-auth)
const auth = getAuth(authApp);
// Get firestore from toto-bo's own database
const db = getFirestore(app);
Security Considerationsβ
- Token Verification: Verified against the respective toto-app environment's Firebase Auth
- Data Isolation: Each environment has its own separate database
- Access Control: Firestore security rules apply per environment
- No Cross-Contamination: Production and staging environments are completely isolated
Benefitsβ
- β Environment Isolation: Each environment has independent authentication
- β Cross-Project Integration: Backoffice can authenticate users from the main app
- β Data Safety: Production and staging data remain completely separate
- β Consistent Experience: Same authentication flow within each environment
Migration Notesβ
When migrating from the old pattern (shared production auth):
- Update
FIREBASE_AUTH_PROJECT_IDin staging to usetoto-f9d2f-stg - Update frontend Firebase config (
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,NEXT_PUBLIC_FIREBASE_PROJECT_ID) in staging - Update Firebase Admin initialization code comments
- Verify token verification works correctly in both environments
- Test that staging uses staging auth and production uses production auth