Skill v1.0.0
currentAutomated scan100/100name: firebase-app-platform description: Build and operate apps on Firebase using Auth, Firestore, Cloud Functions, and Hosting. Use when building mobile/web backends with managed services, real-time data sync, or serverless APIs. category: devops risk: critical source: https://github.com/BagelHole/DevOps-Security-Agent-Skills source_repo: BagelHole/DevOps-Security-Agent-Skills source_type: community date_added: '2026-09-20' license: MIT license_source: https://github.com/BagelHole/DevOps-Security-Agent-Skills/blob/main/LICENSE compatibility: Requires the relevant OS/platform tooling and privileged access where noted. Docs-only; helper scripts and templates not bundled. metadata: author: devops-skills version: '1.0'
Firebase App Platform
Ship mobile and web backends with Firebase managed services.
When to Use This Skill
Use this skill when:
- Building mobile or web apps with real-time data sync
- Need authentication with minimal backend code
- Prototyping quickly with managed infrastructure
- Building serverless APIs with Cloud Functions
- Hosting static sites or SPAs with CDN
Prerequisites
- Node.js 18+
- Firebase CLI (
npm install -g firebase-tools) - Google Cloud account (Firebase is part of GCP)
- A Firebase project (create at console.firebase.google.com)
Security and deployment boundaries
Test rules with allowed/denied identities in the emulator before authorized deployment to an explicitly selected project. User-editable profile fields must never grant admin permissions; the admin example uses a server-managed custom claim. The message rules are an intentionally public-to-authenticated example, not private-channel membership enforcement. Admin SDK HTTP functions bypass Firestore rules, so authenticate/authorize there or expose only explicitly public fields. CORS is not authorization. Firestore triggers may retry/out-of-order: the counter/timestamp illustration needs event-ID deduplication and monotonic timestamp handling before production. Keep secrets in supported secret storage and verify current runtime/config migration guidance; budget alerts do not cap spend. See rule conditions.
Quick Start
# Install and authenticatenpm install -g firebase-toolsfirebase login# Initialize in your project directoryfirebase init# Select: Firestore, Functions, Hosting, Emulators# Start local emulatorsfirebase emulators:start# Deploy everythingfirebase deploy# Deploy specific servicesfirebase deploy --only functionsfirebase deploy --only hostingfirebase deploy --only firestore:rules
Firestore Database
Security Rules
// firestore.rulesrules_version = '2';service cloud.firestore {match /databases/{database}/documents {// Users can only read/write their own datamatch /users/{userId} {allow read, write: if request.auth != null && request.auth.uid == userId;}// Messages: authenticated users can read, only owner can writematch /channels/{channelId}/messages/{messageId} {allow read: if request.auth != null;allow create: if request.auth != null&& request.resource.data.userId == request.auth.uid&& request.resource.data.body is string&& request.resource.data.body.size() <= 5000;allow update: if request.auth != null&& resource.data.userId == request.auth.uid&& request.resource.data.userId == resource.data.userId&& request.resource.data.body is string&& request.resource.data.body.size() <= 5000;allow delete: if request.auth != null&& resource.data.userId == request.auth.uid;}// Admin-only collectionmatch /admin/{document=**} {allow read, write: if request.auth != null&& request.auth.token.admin == true; // custom claim set only by trusted server admin}// Default: deny everythingmatch /{document=**} {allow read, write: if false;}}}
Data Operations
// lib/firestore.tsimport { getFirestore, collection, doc, setDoc, getDoc,query, where, orderBy, limit, onSnapshot,serverTimestamp, increment } from "firebase/firestore";const db = getFirestore();// Create document with auto-IDasync function createMessage(channelId: string, body: string, userId: string) {const ref = doc(collection(db, "channels", channelId, "messages"));await setDoc(ref, {body,userId,createdAt: serverTimestamp(),});return ref.id;}// Real-time listenerfunction subscribeToMessages(channelId: string, callback: (msgs: any[]) => void) {const q = query(collection(db, "channels", channelId, "messages"),orderBy("createdAt", "desc"),limit(50));return onSnapshot(q, (snapshot) => {const messages = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));callback(messages);});}// Atomic counterasync function incrementViews(postId: string) {await setDoc(doc(db, "posts", postId), {views: increment(1),}, { merge: true });}
Indexes
// firestore.indexes.json{"indexes": [{"collectionGroup": "messages","queryScope": "COLLECTION","fields": [{ "fieldPath": "channelId", "order": "ASCENDING" },{ "fieldPath": "createdAt", "order": "DESCENDING" }]}]}
Authentication
// lib/auth.tsimport { getAuth, signInWithPopup, GoogleAuthProvider,createUserWithEmailAndPassword, signInWithEmailAndPassword,signOut, onAuthStateChanged } from "firebase/auth";const auth = getAuth();// Google sign-inasync function signInWithGoogle() {const provider = new GoogleAuthProvider();const result = await signInWithPopup(auth, provider);return result.user;}// Email/password registrationasync function register(email: string, password: string) {const result = await createUserWithEmailAndPassword(auth, email, password);return result.user;}// Auth state listeneronAuthStateChanged(auth, (user) => {if (user) {console.log("Signed in:", user.uid, user.email);} else {console.log("Signed out");}});
Cloud Functions
// functions/src/index.tsimport { onRequest } from "firebase-functions/v2/https";import { onDocumentCreated } from "firebase-functions/v2/firestore";import { getFirestore, FieldValue } from "firebase-admin/firestore";import { initializeApp } from "firebase-admin/app";initializeApp();const db = getFirestore();// HTTP function (API endpoint)export const api = onRequest({ cors: true, region: "us-central1" }, async (req, res) => {if (req.method !== "GET") {res.status(405).send("Method not allowed");return;}const snapshot = await db.collection("posts").orderBy("createdAt", "desc").limit(10).get();const posts = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));res.json({ posts });});// Firestore trigger — runs when a new message is createdexport const onMessageCreated = onDocumentCreated("channels/{channelId}/messages/{messageId}",async (event) => {const data = event.data?.data();if (!data) return;// Update channel's last message timestampawait db.doc(`channels/${event.params.channelId}`).update({lastMessageAt: data.createdAt,messageCount: FieldValue.increment(1),});// Send notification (example)// Do not log message bodies; emit a redacted event identifier only.});
Hosting
// firebase.json{"hosting": {"public": "dist","ignore": ["firebase.json", "**/.*", "**/node_modules/**"],"rewrites": [{ "source": "/api/**", "function": "api" },{ "source": "**", "destination": "/index.html" }],"headers": [{"source": "**/*.@(js|css|svg|png|jpg|webp|woff2)","headers": [{ "key": "Cache-Control", "value": "public, max-age=31536000, immutable" }]},{"source": "**","headers": [{ "key": "X-Frame-Options", "value": "DENY" },{ "key": "X-Content-Type-Options", "value": "nosniff" },{ "key": "Strict-Transport-Security", "value": "max-age=63072000" }]}]}}
Local Emulators
# Start all emulatorsfirebase emulators:start# Start specific emulatorsfirebase emulators:start --only auth,firestore,functions# Export emulator data for persistencefirebase emulators:export ./emulator-datafirebase emulators:start --import=./emulator-data# Emulator UI at http://localhost:4000
// firebase.json — emulator config{"emulators": {"auth": { "port": 9099 },"firestore": { "port": 8080 },"functions": { "port": 5001 },"hosting": { "port": 5000 },"ui": { "enabled": true, "port": 4000 }}}
Environment Configuration
# Set environment variables for functionsfirebase functions:config:set stripe.key="sk_live_xxx" app.name="MyApp"# View configfirebase functions:config:get# Use in functions (v1)const stripeKey = functions.config().stripe.key;# For v2 functions, use .env files# functions/.envSTRIPE_KEY=sk_live_xxx# functions/.env.local (for emulators)STRIPE_KEY=sk_test_xxx
Multi-Environment Setup
# Create separate projects for each environmentfirebase use --add # Add staging project aliasfirebase use staging # Switch to stagingfirebase use production# Deploy to specific projectfirebase deploy --project my-app-stagingfirebase deploy --project my-app-production# .firebaserc{"projects": {"staging": "my-app-staging","production": "my-app-production"}}
CLI Reference
firebase projects:list # List all projectsfirebase deploy # Deploy everythingfirebase deploy --only functions # Deploy only functionsfirebase deploy --only hosting # Deploy only hostingfirebase deploy --only firestore # Deploy rules + indexesfirebase functions:log # View function logsfirebase hosting:channel:create pr-123 # Preview channelfirebase hosting:channel:delete pr-123
Security Best Practices
- Write strict Firestore security rules before any other code
- Separate environments by Firebase project (staging/production)
- Enable budget alerts and quota monitoring in GCP console
- Move privileged logic into Cloud Functions (never trust the client)
- Use App Check to prevent API abuse from non-app clients
- Enable Firestore audit logging for compliance
- Review OAuth consent screen settings
Troubleshooting
| Issue | Solution | |
|---|---|---|
| Permission denied | Check Firestore rules, verify auth state | |
| Function cold starts | Use min instances (minInstances: 1), optimize imports | |
| Emulator won't start | Check port conflicts, run firebase emulators:start --debug | |
| Deploy fails | Run firebase deploy --debug, check service account permissions | |
| Rules test failing | Use firebase emulators:exec to run rules unit tests |
Related Skills
- gcp-cloud-functions (
gcp-cloud-functions) — Function runtime patterns - vercel-deployments (
vercel-deployments) — Alternative frontend hosting - convex-backend (
convex-backend) — Alternative managed backend
Limitations
- Infrastructure commands can disrupt services: confirm target host/scope and have backups/snapshots before mutating state.
- Docs-only import: upstream scripts and templates not bundled.