Quick Reference Firebase Cheatsheet
Firebase on one page — Auth, Firestore reads and writes, listeners,
security rules, indexes, and the traps around setDoc and the apiKey.
10 sections 10 tables 14 code snippets Install npm install firebase One app, two services import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getFirestore } from "firebase/firestore";
const app = initializeApp({
apiKey: "YOUR_KEY",
authDomain: "YOUR_PROJECT.firebaseapp.com",
projectId: "YOUR_PROJECT",
});
const auth = getAuth(app);
const db = getFirestore(app); This page is Authentication and Cloud Firestore. The Realtime Database and the Admin SDK are different products. Email account and the session import {
createUserWithEmailAndPassword,
onAuthStateChanged,
signOut,
} from "firebase/auth";
const cred = await createUserWithEmailAndPassword(auth, email, password);
onAuthStateChanged(auth, (user) => {
if (user) console.log(user.uid);
});
await signOut(auth); Create with your own id import { doc, setDoc } from "firebase/firestore";
await setDoc(doc(db, "books", "dune"), {
title: "Dune",
pages: 412,
}); Change one field import { doc, updateDoc } from "firebase/firestore";
await updateDoc(doc(db, "books", "dune"), {
price: 19,
}); One document import { doc, getDoc } from "firebase/firestore";
const snap = await getDoc(doc(db, "books", "dune"));
if (snap.exists()) console.log(snap.data()); A filtered list import { collection, query, where, orderBy, limit, getDocs } from "firebase/firestore";
const q = query(
collection(db, "books"),
where("pages", ">=", 300),
orderBy("pages", "desc"),
limit(10)
);
const snaps = await getDocs(q); Stay subscribed import { collection, onSnapshot, query, where } from "firebase/firestore";
const q = query(collection(db, "books"), where("pages", ">=", 300));
const stop = onSnapshot(q, (snaps) => {
snaps.forEach((snap) => console.log(snap.id));
}); Sentinels, not a read-then-write import { doc, updateDoc, increment, arrayUnion, serverTimestamp, deleteField } from "firebase/firestore";
await updateDoc(doc(db, "books", "dune"), {
stock: increment(1),
tags: arrayUnion("scifi"),
updated: serverTimestamp(),
draft: deleteField(),
}); Two writes, one outcome import { doc, writeBatch } from "firebase/firestore";
const batch = writeBatch(db);
batch.update(doc(db, "books", "dune"), { price: 19 });
batch.set(doc(db, "orders", "o1"), { bookId: "dune", amount: 19 });
await batch.commit(); Only signed-in owners can write service cloud.firestore {
match /databases/{database}/documents {
match /books/{bookId} {
allow read: if request.auth != null;
allow write: if request.auth != null
&& request.auth.uid == request.resource.data.owner;
}
}
} 📇 Indexes and document shape This query needs a composite index query(
collection(db, "books"),
where("author", "==", uid),
orderBy("pages", "desc")
); The session onAuthStateChanged(auth, (user) => {
console.log(user && user.uid);
}); Keep the other fields await setDoc(ref, { price: 19 }, { merge: true }); How to use this page: install firebase, call initializeApp once, and wait for onAuthStateChanged before you read uid. Then setDoc a book, query it with where and orderBy, and publish rules that match that query. |