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, auth flows, cloud functions, security rules, SDK setup, and real-time data. Use when working with firebase patterns, integrating firebase.4license: Apache-2.05---6789## Overview1011Firebase-specific patterns for building scalable applications. Covers Firestore data modeling, security rules, Cloud Functions, and auth flows.1213## Capabilities1415- Design Firestore data models for efficient queries16- Write security rules for fine-grained access control17- Build Cloud Functions for serverless backends18- Implement Firebase Auth with social providers19- Use Firebase Storage for file uploads2021## When to Use22**Trigger phrases:**23- "firebase integration"24- "Integrate Firebase for authentication, Firestore database, Cloud Functions, host"25- "firebase patterns"26- "Firebase patterns — Firestore queries, auth flows, cloud functions, and security"272829- Building real-time apps with Firebase30- Need offline-first mobile/web app31- Serverless backend with Cloud Functions32- Social auth integration (Google, Apple, GitHub)3334## Workflow35361. **Set up project** - Create Firebase project, add apps372. **Install SDK** - `npm install firebase` or use modular SDK383. **Configure auth** - Enable providers, set up sign-in flows394. **Design data model** - Firestore collections, documents, subcollections405. **Build queries** - Real-time listeners, compound queries, pagination416. **Deploy functions** - Cloud Functions for backend logic4243## Code Example (JavaScript)4445```javascript46import { initializeApp } from 'firebase/app';47import { getFirestore, collection, onSnapshot, query, where } from 'firebase/firestore';48import { getAuth, signInWithPopup, GoogleAuthProvider } from 'firebase/auth';4950const app = initializeApp(firebaseConfig);51const db = getFirestore(app);52const auth = getAuth(app);5354const { user } = await signInWithPopup(auth, new GoogleAuthProvider());5556const q = query(collection(db, 'messages'), where('channel', '==', 'general'));57const unsubscribe = onSnapshot(q, (snapshot) => {58 snapshot.docChanges().forEach((change) => {59 if (change.type === 'added') console.log('New message:', change.doc.data());60 });61});62```6364## When NOT to Use6566- Task is about deployment, not development (use deploy skills)67- Task is about code review, not writing (use review skills)68- You need to understand existing code first (use research skills)69- Task is about testing only (use test skills)70- Requirements are unclear (clarify first)71- Task is trivially simple (single line fix)727374- For relational data (use PostgreSQL/Supabase)75- For complex queries (Firestore has limited query capabilities)7677## Pseudo Code7879The firebase-patterns workflow follows a standard pipeline pattern.8081Core flow:82```83# firebase-patterns primary flow84input = prepare(raw_data)85result = process(input, config={auth, cloud, firebase, firestore, flows})86validate(result)87deliver(result)88```8990Error handling:91```92on error:93 log(error_details)94 retry_with_backoff(max=3)95 if still_failing: alert_and_escalate()96```979899### Firestore Data Model100```javascript101// Denormalized for query efficiency102{103 "users/user1": {104 "name": "John",105 "postCount": 42106 },107 "users/user1/posts/post1": {108 "title": "Hello",109 "createdAt": timestamp110 }111}112```113114### Security Rules115```116rules_version = '2';117service cloud.firestore {118 match /databases/{database}/documents {119 match /users/{userId} {120 allow read: if request.auth != null;121 allow write: if request.auth.uid == userId;122 }123 }124}125```126127## Common Patterns128129- **Denormalize for reads**: Duplicate data to avoid subcollection queries130- **Security rules as tests**: Write rules first, then build features131- **Batch writes**: Use batched writes for multi-document operations132- **Offline persistence**: Enable for mobile apps by default133134## How to Use1351361. Understand the requirement and existing codebase patterns1372. Design the solution with error handling and testability in mind1383. Implement incrementally with tests for each change1394. Verify against expected outcomes (manual and automated)1405. Document usage, edge cases, and integration points1416. Review with team before merging to shared branches142143## Red Flags144145- **Skipping tests to ship faster**: Untested code breaks in production when you least expect it146- **No error handling in production code**: Unhandled errors crash services and lose user data147- **Hardcoded configuration values**: Hardcoded values prevent environment switching and leak secrets148- **Ignoring security implications**: Missing input validation, auth bypasses, and injection vulnerabilities149- **Over-engineering simple solutions**: Premature abstraction adds complexity without proportional benefit150151## Verification152153- [ ] Skill output matches expected behavior154- [ ] Authentication works with configured providers155- [ ] Firestore queries return correct data156- [ ] Real-time listeners fire on data changes157- [ ] Security rules enforce access control158159## Process1601611. Analyze the task requirements1622. Apply domain expertise1633. Verify output quality164165## Anti-Rationalization Table166167| Rationalization | Reality |168|---|---|169| "Tests slow me down" | Bugs slow you down 10x more. Tests are speed, not overhead. |170| "I will refactor later" | Technical debt compounds. Refactor as you go. |171| "It works on my machine" | If it is not in CI, it does not work. Ship proof, not claims. |172| "Firestore is just JSON" | Document/subcollection model requires specific data modeling patterns |173| "I will skip security rules" | Without rules, any client can read/write any document |