Home life · Updated
How to Use Firebase: Set Up, Auth, and Database
Quick answer Firebase is a Google backend that handles authentication, a database, and hosting. You can have a working app connected in about 20 minutes, with the first successful read/write in under an hour.
What You Need Before You Start
You need a Google account (the one you use for Gmail works), a code editor, and a project folder with your app’s files. Firebase works with web, Android, iOS, and Flutter; these steps cover the web SDK, but the console setup is the same for all platforms.
If you are adding Firebase to an existing app, you will also need your app’s package name (Android) or bundle ID (iOS) ready. For web, you just need the folder where your HTML and JavaScript live.
The 7 Steps to a Working Firebase Project
- Create a Firebase project — Go to the Firebase console (console.firebase.google.com) and sign in with your Google account. Click “Add project,” name it (e.g., “my-app”), and follow the prompts. You can skip Google Analytics if you do not need it.
- Register your app — In the project overview, click the web icon (
</>) to add a web app. Give it a nickname (like “web”) and click “Register app.” Firebase shows a code snippet with your config object — copy it or keep the tab open. - Install the Firebase SDK — In your project folder, run
npm install firebaseif you use npm, or add the CDN script tags from the snippet to your HTML. The snippet includesfirebase-appandfirebase-analytics; you will add more modules later for auth and database. - Initialize Firebase in your code — Create a file (e.g.,
firebase.js) and paste the config object from step 2. Then addimport { initializeApp } from "firebase/app";andconst app = initializeApp(firebaseConfig);. This connects your code to your project. - Enable Authentication — In the Firebase console, go to “Build > Authentication” and click “Get started.” Under “Sign-in method,” enable the providers you want (Email/Password is easiest to start). Add a test user with an email and password.
- Add a database — Go to “Build > Firestore Database” (or Realtime Database) and click “Create database.” Choose production mode for now, then pick a location. Firestore is recommended for new projects.
- Write your first read/write — In your code, import
getFirestoreandcollection,addDocfrom “firebase/firestore”. UseaddDoc(collection(db, "items"), {name: "test"})to add a document, andonSnapshotto listen for changes. Test it by opening your page and checking the console for the document.
How to Tell It Worked
After step 7, open your app in the browser. If you added a document, go to the Firestore console and click on the “items” collection — you should see a document with name: "test". For authentication, try signing in with the test user; the console will show the user’s UID in the “Users” tab.
If you see no errors in the browser console and the document appears, your setup is correct. If you are using the modular SDK, you should also see a network request in the browser’s Network tab to firestore.googleapis.com.
The One Failure People Hit: Rules Blocking Reads
By default, Firestore rules deny all reads and writes. If your code tries to read or write and you get a “Missing or insufficient permissions” error, that is why.
Fix it by going to “Build > Firestore Database > Rules” and changing the rules to allow test access. For development only, use:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
Publish those rules, then retry. Remember to lock them down before production — set if request.auth != null or use more specific matches.
Authentication in 3 Commands
Email/password auth is the fastest to set up. After enabling it in the console, add this to your code:
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword } from "firebase/auth";
const auth = getAuth();
// Sign up
createUserWithEmailAndPassword(auth, email, password);
// Sign in
signInWithEmailAndPassword(auth, email, password);
To check if a user is signed in, use onAuthStateChanged(auth, (user) => { ... }). That callback fires once on load and every time the sign-in state changes. For Google sign-in, enable the Google provider in the console and use signInWithPopup(auth, new GoogleAuthProvider()).
If you want to protect routes or pages, check user inside onAuthStateChanged and redirect if it is null.
Firestore vs. Realtime Database: Which One to Pick
Firestore is the newer, more feature-rich database with better querying and a more intuitive data model. Realtime Database is simpler and better for real-time sync of small, frequent updates like chat messages or presence indicators.
| Feature | Firestore | Realtime Database |
|---|---|---|
| Data model | Documents and collections | JSON tree |
| Queries | Compound queries, filters | Basic filtering and ordering |
| Real-time sync | Yes, with onSnapshot | Yes, with on() |
| Scaling | Automatic, strong consistency | Simpler, but limited |
| Free tier | 50K reads/day | 100K simultaneous connections |
For most new projects, start with Firestore. Choose Realtime Database only if you need very low latency for frequent, small updates.
Deploying Your Firebase App for Free
Firebase Hosting gives you a free domain (your-project.web.app) and serves your static files over HTTPS. To deploy:
- Install the Firebase CLI:
npm install -g firebase-tools. - Run
firebase loginand sign in with your Google account. - In your project folder, run
firebase init hosting. Choose your project, set the public directory topublic(ordistif you use a build step), and accept the default for the single-page app question. - Build your app if needed (e.g.,
npm run build), then runfirebase deploy.
After deployment, visit the URL shown in the output. Your app is live. Every time you make changes, run firebase deploy again — it only uploads changed files.
Security Rules: What to Lock Down First
Before you go live, change your Firestore rules from “allow all” to something that requires authentication. A common starting rule for a user’s own data:
match /users/{userId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
For collections that are public (like a blog), use allow read: if true; allow write: if request.auth != null;. For Realtime Database, the equivalent is:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
Test your rules by opening your app in an incognito window and trying to read or write. If you get a permission error, that means the rules are working as intended.
Frequently Asked Questions
Do I need to pay for Firebase?
No. The Spark plan (free tier) includes 50,000 reads, 20,000 writes, and 10,000 deletes per day in Firestore, plus 1 GB of storage. Authentication is free for the first 50,000 monthly active users. You only pay if you exceed those limits or enable paid features like Cloud Functions.
Can I use Firebase with React or Vue?
Yes. The web SDK works with any JavaScript framework. Install it with npm, then import the functions you need in your components. For React, you might use a context provider to share the auth state.
How do I reset a forgotten password?
Use the sendPasswordResetEmail(auth, email) function from the Firebase auth SDK. This sends an email with a link to reset the password. You can also trigger it from a button in your UI.
What if I get “Firebase: Error (auth/email-already-in-use)”?
That error means the email is already registered. Catch it in your sign-up code and show a message telling the user to sign in instead. You can also check with fetchSignInMethodsForEmail(auth, email) before creating the account.
Related
- How to Give Access to a Firebase Project
- Australian Privacy Principles: Full List & Guide
- AT&T Privacy Center Number: Direct Help Line
Sources: Google Firebase Documentation. Menus and buttons can differ slightly by version. Spotted something out of date? Tell us.