Securing Firestore and Realtime Database

Tutorial 4 of 5

1. Introduction

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

2. Step-by-Step Guide

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.

Understanding Firebase Security Rules

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;
    }
  }
}

Writing and Applying Firebase Security Rules

To write and apply Firebase Security rules, you can use the Firebase console. Here's how:

  1. Navigate to your Firebase console, select your project, and then select the "Database" option.
  2. In the Database section, choose "Rules" in the tab.
  3. Here you can edit your rules. After editing, press "Publish" to apply the changes.

Remember to always test your rules with the "Rules Playground" in Firebase Console before deploying them.

3. Code Examples

Let's look at some practical examples.

Example 1: Rules that permit authenticated access

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;
    }
  }
}

Example 2: Rules that allow public read access but authenticated write access

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.

4. Summary

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

5. Practice Exercises

  1. Write a rule that allows only the owner of a document to read and write it.

    • Hint: Assume each document has a 'uid' field that holds the owner's user id.
  2. Write a rule that allows anyone to read data, but only authenticated users with email verified to write data.

    • Hint: Use 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.