Firebase to platforma Backend-as-a-Service (BaaS) od Google. Oferuje gotowe usługi backendowe: bazę danych NoSQL (Firestore), autentykację, storage, hosting, Cloud Functions, analytics, push notifications i wiele więcej.
Firebase pozwala zbudować pełny backend bez pisania serwera. Idealny do MVP, startupów i aplikacji real-time. Skaluje się automatycznie i oferuje model pay-as-you-go z hojnym darmowym tier.
import { collection, doc, addDoc, getDocs, query, where, orderBy } from 'firebase/firestore';
// Dodaj użytkownika
const createUser = async (data: CreateUserDTO) => {
const ref = await addDoc(collection(db, 'users'), {
...data,
createdAt: serverTimestamp(),
});
return ref.id;
};
// Pobierz aktywnych użytkowników
const getActiveUsers = async () => {
const q = query(
collection(db, 'users'),
where('isActive', '==', true),
orderBy('createdAt', 'desc'),
);
const snap = await getDocs(q);
return snap.docs.map(d => ({ id: d.id, ...d.data() }));
};
import { signInWithPopup, GoogleAuthProvider, onAuthStateChanged } from 'firebase/auth';
// Google Sign-In
const signInWithGoogle = async () => {
const provider = new GoogleAuthProvider();
const result = await signInWithPopup(auth, provider);
return result.user;
};
// Obserwuj stan autentykacji
onAuthStateChanged(auth, (user) => {
if (user) {
console.log('Zalogowany:', user.displayName);
} else {
console.log('Wylogowany');
}
});
import { onDocumentCreated } from 'firebase-functions/v2/firestore';
import { getMessaging } from 'firebase-admin/messaging';
// Trigger: nowy zamówienie → wyślij push
export const onNewOrder = onDocumentCreated(
'orders/{orderId}',
async (event) => {
const order = event.data?.data();
if (!order) return;
await getMessaging().send({
token: order.userFcmToken,
notification: {
title: 'Nowe zamówienie!',
body: `Zamówienie #${event.params.orderId} zostało złożone.`,
},
});
}
);
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Użytkownik widzi tylko swoje dane
match /users/{userId} {
allow read: if request.auth.uid == userId;
allow write: if request.auth.uid == userId
&& request.resource.data.keys()
.hasOnly(['name', 'avatar', 'bio']);
}
// Admin widzi wszystko
match /{document=**} {
allow read, write: if request.auth.token.admin == true;
}
}
}
NoSQL, real-time sync, offline support, auto-scaling. Kolekcje, dokumenty, subkolekcje.
Google, Apple, Facebook, email/password, phone, anonymous. MFA, custom claims, SSO.
Serverless backend. Triggers (Firestore, Auth, Storage), HTTP callable, scheduled.
Upload plików, obrazów, video. Resize, thumbnails. Security rules per-file.
CDN, SSL, custom domains. Preview channels, rollbacks. Idealne do SPA i SSR.
Event tracking, user properties, funnels. Crash reporting, ANR detection.
Idealny gdy: budujesz MVP, potrzebujesz real-time sync, chcesz szybki start bez pisania backendu, budujesz chat, social app, marketplace z auth i storage.
Nie najlepszy gdy: potrzebujesz złożonych relacji SQL (lepiej PostgreSQL), chcesz uniknąć vendor lock-in (lepiej Supabase), budujesz heavy-compute backend (lepiej Go/Python).