Overview
Firebase-specific patterns for building scalable applications. Covers Firestore data modeling, security rules, Cloud Functions, and auth flows.
Capabilities
- Design Firestore data models for efficient queries
- Write security rules for fine-grained access control
- Build Cloud Functions for serverless backends
- Implement Firebase Auth with social providers
- Use Firebase Storage for file uploads
When to Use
Trigger phrases:
"firebase integration"
"Integrate Firebase for authentication, Firestore database, Cloud Functions, host"
"firebase patterns"
"Firebase patterns — Firestore queries, auth flows, cloud functions, and security"
Building real-time apps with Firebase
Need offline-first mobile/web app
Serverless backend with Cloud Functions
Social auth integration (Google, Apple, GitHub)
Workflow
- Set up project - Create Firebase project, add apps
- Install SDK -
npm install firebase or use modular SDK
- Configure auth - Enable providers, set up sign-in flows
- Design data model - Firestore collections, documents, subcollections
- Build queries - Real-time listeners, compound queries, pagination
- Deploy functions - Cloud Functions for backend logic
Code Example (JavaScript)
import { initializeApp } from 'firebase/app';
import { getFirestore, collection, onSnapshot, query, where } from 'firebase/firestore';
import { getAuth, signInWithPopup, GoogleAuthProvider } from 'firebase/auth';
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const auth = getAuth(app);
const { user } = await signInWithPopup(auth, new GoogleAuthProvider());
const q = query(collection(db, 'messages'), where('channel', '==', 'general'));
const unsubscribe = onSnapshot(q, (snapshot) => {
snapshot.docChanges().forEach((change) => {
if (change.type === 'added') console.log('New message:', change.doc.data());
});
});
When NOT to Use
Task is about deployment, not development (use deploy skills)
Task is about code review, not writing (use review skills)
You need to understand existing code first (use research skills)
Task is about testing only (use test skills)
Requirements are unclear (clarify first)
Task is trivially simple (single line fix)
For relational data (use PostgreSQL/Supabase)
For complex queries (Firestore has limited query capabilities)
Pseudo Code
The firebase-patterns workflow follows a standard pipeline pattern.
Core flow:
# firebase-patterns primary flow
input = prepare(raw_data)
result = process(input, config={auth, cloud, firebase, firestore, flows})
validate(result)
deliver(result)
Error handling:
on error:
log(error_details)
retry_with_backoff(max=3)
if still_failing: alert_and_escalate()
Firestore Data Model
// Denormalized for query efficiency
{
"users/user1": {
"name": "John",
"postCount": 42
},
"users/user1/posts/post1": {
"title": "Hello",
"createdAt": timestamp
}
}
Security Rules
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read: if request.auth != null;
allow write: if request.auth.uid == userId;
}
}
}
Common Patterns
- Denormalize for reads: Duplicate data to avoid subcollection queries
- Security rules as tests: Write rules first, then build features
- Batch writes: Use batched writes for multi-document operations
- Offline persistence: Enable for mobile apps by default
How to Use
- Understand the requirement and existing codebase patterns
- Design the solution with error handling and testability in mind
- Implement incrementally with tests for each change
- Verify against expected outcomes (manual and automated)
- Document usage, edge cases, and integration points
- Review with team before merging to shared branches
Red Flags
- Skipping tests to ship faster: Untested code breaks in production when you least expect it
- No error handling in production code: Unhandled errors crash services and lose user data
- Hardcoded configuration values: Hardcoded values prevent environment switching and leak secrets
- Ignoring security implications: Missing input validation, auth bypasses, and injection vulnerabilities
- Over-engineering simple solutions: Premature abstraction adds complexity without proportional benefit
Verification
Process
- Analyze the task requirements
- Apply domain expertise
- Verify output quality
Anti-Rationalization Table
| Rationalization |
Reality |
| "Tests slow me down" |
Bugs slow you down 10x more. Tests are speed, not overhead. |
| "I will refactor later" |
Technical debt compounds. Refactor as you go. |
| "It works on my machine" |
If it is not in CI, it does not work. Ship proof, not claims. |
| "Firestore is just JSON" |
Document/subcollection model requires specific data modeling patterns |
| "I will skip security rules" |
Without rules, any client can read/write any document |
1---2name: firebase-patterns3description: Use when firebase patterns and integration — Firestore queries, auth4 flows, cloud functions, security rules, SDK setup, and real-time data. Use when5 working with firebase patterns, integrating firebase.6domain: development7author: oyi778license: Apache-2.09subdomain: software-development10tags:11- coding12- firebase13- firestore14- auth15- cloud-functions16- realtime17- patterns18- rest-api19- software-engineering20- testing21version: 1.0.022category: development23metadata:24 source: https://skillmd.com/skills/oyi77/firebase-patterns25---26272829## Overview3031Firebase-specific patterns for building scalable applications. Covers Firestore data modeling, security rules, Cloud Functions, and auth flows.3233## Capabilities3435- Design Firestore data models for efficient queries36- Write security rules for fine-grained access control37- Build Cloud Functions for serverless backends38- Implement Firebase Auth with social providers39- Use Firebase Storage for file uploads4041## When to Use42**Trigger phrases:**43- "firebase integration"44- "Integrate Firebase for authentication, Firestore database, Cloud Functions, host"45- "firebase patterns"46- "Firebase patterns — Firestore queries, auth flows, cloud functions, and security"474849- Building real-time apps with Firebase50- Need offline-first mobile/web app51- Serverless backend with Cloud Functions52- Social auth integration (Google, Apple, GitHub)5354## Workflow55561. **Set up project** - Create Firebase project, add apps572. **Install SDK** - `npm install firebase` or use modular SDK583. **Configure auth** - Enable providers, set up sign-in flows594. **Design data model** - Firestore collections, documents, subcollections605. **Build queries** - Real-time listeners, compound queries, pagination616. **Deploy functions** - Cloud Functions for backend logic6263## Code Example (JavaScript)6465```javascript66import { initializeApp } from 'firebase/app';67import { getFirestore, collection, onSnapshot, query, where } from 'firebase/firestore';68import { getAuth, signInWithPopup, GoogleAuthProvider } from 'firebase/auth';6970const app = initializeApp(firebaseConfig);71const db = getFirestore(app);72const auth = getAuth(app);7374const { user } = await signInWithPopup(auth, new GoogleAuthProvider());7576const q = query(collection(db, 'messages'), where('channel', '==', 'general'));77const unsubscribe = onSnapshot(q, (snapshot) => {78 snapshot.docChanges().forEach((change) => {79 if (change.type === 'added') console.log('New message:', change.doc.data());80 });81});82```8384## When NOT to Use8586- Task is about deployment, not development (use deploy skills)87- Task is about code review, not writing (use review skills)88- You need to understand existing code first (use research skills)89- Task is about testing only (use test skills)90- Requirements are unclear (clarify first)91- Task is trivially simple (single line fix)929394- For relational data (use PostgreSQL/Supabase)95- For complex queries (Firestore has limited query capabilities)9697## Pseudo Code9899The firebase-patterns workflow follows a standard pipeline pattern.100101Core flow:102```103# firebase-patterns primary flow104input = prepare(raw_data)105result = process(input, config={auth, cloud, firebase, firestore, flows})106validate(result)107deliver(result)108```109110Error handling:111```112on error:113 log(error_details)114 retry_with_backoff(max=3)115 if still_failing: alert_and_escalate()116```117118119### Firestore Data Model120```javascript121// Denormalized for query efficiency122{123 "users/user1": {124 "name": "John",125 "postCount": 42126 },127 "users/user1/posts/post1": {128 "title": "Hello",129 "createdAt": timestamp130 }131}132```133134### Security Rules135```136rules_version = '2';137service cloud.firestore {138 match /databases/{database}/documents {139 match /users/{userId} {140 allow read: if request.auth != null;141 allow write: if request.auth.uid == userId;142 }143 }144}145```146147## Common Patterns148149- **Denormalize for reads**: Duplicate data to avoid subcollection queries150- **Security rules as tests**: Write rules first, then build features151- **Batch writes**: Use batched writes for multi-document operations152- **Offline persistence**: Enable for mobile apps by default153154## How to Use1551561. Understand the requirement and existing codebase patterns1572. Design the solution with error handling and testability in mind1583. Implement incrementally with tests for each change1594. Verify against expected outcomes (manual and automated)1605. Document usage, edge cases, and integration points1616. Review with team before merging to shared branches162163## Red Flags164165- **Skipping tests to ship faster**: Untested code breaks in production when you least expect it166- **No error handling in production code**: Unhandled errors crash services and lose user data167- **Hardcoded configuration values**: Hardcoded values prevent environment switching and leak secrets168- **Ignoring security implications**: Missing input validation, auth bypasses, and injection vulnerabilities169- **Over-engineering simple solutions**: Premature abstraction adds complexity without proportional benefit170171## Verification172173- [ ] Skill output matches expected behavior174- [ ] Authentication works with configured providers175- [ ] Firestore queries return correct data176- [ ] Real-time listeners fire on data changes177- [ ] Security rules enforce access control178179## Process1801811. Analyze the task requirements1822. Apply domain expertise1833. Verify output quality184185## Anti-Rationalization Table186187| Rationalization | Reality |188|---|---|189| "Tests slow me down" | Bugs slow you down 10x more. Tests are speed, not overhead. |190| "I will refactor later" | Technical debt compounds. Refactor as you go. |191| "It works on my machine" | If it is not in CI, it does not work. Ship proof, not claims. |192| "Firestore is just JSON" | Document/subcollection model requires specific data modeling patterns |193| "I will skip security rules" | Without rules, any client can read/write any document |