In this tutorial, we will focus on securing your Firestore and Realtime Database using Firebase Security Rules. The goal is to help you understand, write, and apply security rules to your Firestore and Realtime Database to ensure data safety and integrity.
By the end of the tutorial, you will learn:
- How Firebase Security Rules work
- How to write and apply Firebase Security Rules to Firestore and Realtime Database
- Best practices for writing security rules
Prerequisites:
- Basic understanding of Firebase
- A Firebase project setup with Firestore or Realtime Database initialized
Firebase Security Rules provide the first line of defense for your Firebase data. These rules determine who has read and write access to your database, validate incoming data, and control how data can be indexed.
Firebase Security Rules are written in an expressive and flexible syntax that allows for complex rule sets. The rules are structured with a JSON-like syntax, and each rule consists of a match statement and an allow statement.
For instance, this is a basic rule that allows anyone to read and write data:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;
}
}
}
To write and apply Firebase Security rules, you can use the Firebase console. Here's how:
Remember to always test your rules with the "Rules Playground" in Firebase Console before deploying them.
Let's look at some practical examples.
This rule allows only authenticated users (users logged in via Firebase Authentication) to read and write data:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
This rule allows anyone to read data, but only authenticated users to write data:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read;
allow write: if request.auth != null;
}
}
}
Note: request.auth
holds information about the user making the request, and request.auth.uid
holds the authenticated user's uid.
In this tutorial, you've learned the basics of Firebase Security Rules, how to write and apply them to Firestore and Realtime Database. Remember to always test your rules before applying them to avoid breaking your application.
Next steps for learning:
- Learn more about Firebase Security Rules syntax and structure
- Explore Firebase Security Rules samples
Write a rule that allows only the owner of a document to read and write it.
Write a rule that allows anyone to read data, but only authenticated users with email verified to write data.
request.auth.token.email_verified
Solutions:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null && request.auth.uid == resource.data.uid;
}
}
}
Explanation: This rule checks if the user is authenticated and if the authenticated user's uid matches the uid in the document's data.
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read;
allow write: if request.auth != null && request.auth.token.email_verified;
}
}
}
Explanation: This rule allows anyone to read data. For write operations, it checks if the user is authenticated and if the user's email is verified.
For further practice, experiment with different rule sets to cover various use cases.