📖 Scenario: You are building a simple Firebase database for a small community app. You want to make sure that only authorized users can read and write their own data. This project will help you understand how Firebase security rules protect your data from unauthorized access.
🎯 Goal: Build Firebase security rules that allow users to read and write only their own data in the database.
📋 What You'll Learn
Create a Firebase database structure with user data
Add a variable to identify the current user
Write security rules that allow read and write access only if the user is authenticated and accessing their own data
Complete the security rules to enforce data protection
💡 Why This Matters
🌍 Real World
Firebase security rules are essential to protect user data in real apps, preventing unauthorized access and data leaks.
💼 Career
Understanding and writing Firebase security rules is a key skill for cloud developers and backend engineers working with Firebase.
Progress0 / 4 steps
1
Create the Firebase database structure
Create a Firebase database structure called users where each user has a unique ID and a name field. Use this exact structure: users: { 'user123': { name: 'Alice' }, 'user456': { name: 'Bob' } }
Firebase
Hint
Think of users as a folder with files named by user IDs. Each file holds the user's name.
2
Add a variable for the current user ID
Add a variable called request.auth.uid that represents the current authenticated user's ID in Firebase security rules.
Firebase
Hint
This variable is used in Firebase rules to check who is making the request.
3
Write security rules for user data access
Write Firebase security rules that allow read and write access only if request.auth.uid matches the user ID in the database path. Use match /users/{userId} and check request.auth.uid == userId for both read and write.
Firebase
Hint
This rule means users can only access their own data when logged in.
4
Complete the security rules to protect data
Complete the Firebase security rules by adding the rules_version declaration and enclosing the rules inside service cloud.firestore with the correct database path. Ensure the rules protect the users collection as specified.
Firebase
Hint
These lines are required for Firebase to understand and enforce your rules.
Practice
(1/5)
1. Why are Firebase security rules important for your app's data?
easy
A. They change the app's user interface based on user roles.
B. They speed up the app's performance by caching data.
C. They automatically back up your data to the cloud.
D. They control who can read or write data to keep it safe.
Solution
Step 1: Understand the role of security rules
Security rules are designed to control access to data, deciding who can read or write it.
Step 2: Identify the main purpose of these rules
The main goal is to keep data safe and private by restricting unauthorized access.
Final Answer:
They control who can read or write data to keep it safe. -> Option D
Quick Check:
Security rules protect data = They control who can read or write data to keep it safe. [OK]
Hint: Security rules control access to data, not app speed or UI [OK]
Common Mistakes:
Confusing security rules with performance features
Thinking rules change app design or backup data
Assuming rules automatically fix bugs
2. Which of the following is the correct way to write a Firebase security rule that allows only authenticated users to read data?
easy
A. allow read: if request.auth != null;
B. allow read: if user.authenticated == true;
C. allow read: if auth.user == true;
D. allow read: if request.user != null;
Solution
Step 1: Recall Firebase rule syntax for authentication
Firebase uses request.auth != null to check if a user is signed in.
Step 2: Compare options to correct syntax
Only allow read: if request.auth != null; uses the correct Firebase syntax for authenticated access.
But users report they cannot write their own data. What is the likely problem?
medium
A. The rule is missing a semicolon at the end.
B. The rule should use request.resource.data.ownerId instead of resource.data.ownerId.
C. The rule should allow all users to write without conditions.
D. The rule should check request.auth.email instead of UID.
Solution
Step 1: Understand the difference between resource and request.resource
resource.data refers to existing data, while request.resource.data refers to new data being written.
Step 2: Identify the correct field to check ownerId
To verify the ownerId in the data being written, use request.resource.data.ownerId.
Final Answer:
The rule should use request.resource.data.ownerId instead of resource.data.ownerId. -> Option B
Quick Check:
Check new data with request.resource = The rule should use request.resource.data.ownerId instead of resource.data.ownerId. [OK]
Hint: Use request.resource for new data checks, not resource [OK]
Common Mistakes:
Confusing resource with request.resource
Ignoring the need to check new data fields
Removing conditions to fix access issues
5. You want to protect a collection so that users can only read their own documents and write only if they are the owner. Which rule correctly enforces this?
hard
A. allow read: if request.auth.uid == resource.data.ownerId; allow write: if request.auth.uid == request.resource.data.ownerId;
B. allow read: if resource.data.ownerId == request.auth.uid; allow write: if request.auth.uid == resource.data.ownerId;
C. allow read, write: if request.auth != null;
D. allow read, write: if request.auth.uid == resource.data.ownerId && request.auth.uid == request.resource.data.ownerId;
Solution
Step 1: Understand read and write conditions
Reads check existing data's ownerId with resource.data.ownerId. Writes check new data's ownerId with request.resource.data.ownerId.
Step 2: Match conditions to options
allow read: if request.auth.uid == resource.data.ownerId; allow write: if request.auth.uid == request.resource.data.ownerId; correctly separates read and write checks using the right references for existing and new data.
Final Answer:
allow read: if request.auth.uid == resource.data.ownerId; allow write: if request.auth.uid == request.resource.data.ownerId; -> Option A
Quick Check:
Read uses resource, write uses request.resource = allow read: if request.auth.uid == resource.data.ownerId; allow write: if request.auth.uid == request.resource.data.ownerId; [OK]
Hint: Read checks resource, write checks request.resource for ownerId [OK]
Common Mistakes:
Using same data reference for read and write
Allowing all authenticated users to write
Not checking ownership on writes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.