0Pricing
Firebase Auth & Realtime Database Apps · Lekcja

Zrozumienie składni reguł bezpieczeństwa

Poznaj składnię i strukturę Firebase Realtime Database Security Rules służących do definiowania uprawnień dostępu.

Zrozumienie składni reguł bezpieczeństwa to bezpłatna lekcja Firebase Auth & Realtime Database Apps na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Firebase Auth & Realtime Database Apps, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Firebase Auth & Realtime Database Apps zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Why Security Rules?

Welcome to Realtime Database Security Rules! These rules are super important for keeping your data safe and controlling who can do what in your Firebase app.

Think of them as bouncers for your database: they check every request to read, write, or update data, and decide if it's allowed or not.

Your Rules File

Firebase Realtime Database Security Rules are defined in a JSON file, usually named rules.json. You'll upload this file to your Firebase project.

The entire set of rules is wrapped under a top-level "rules" key, like this:

{
  "rules": {
    // Your security rules go here!
  }
}

Default Open Rules

When you first create a Realtime Database, Firebase often provides a very open set of rules. This allows anyone to read and write data, which is great for getting started quickly, but terrible for production!

These rules look like this:

{
  "rules": {
    ".read": "true",
    ".write": "true"
  }
}

Targeting Data Paths

Rules are applied based on the path to your data. You nest rules within the "rules" object to target specific parts of your database, just like folders in a file system.

For example, to set rules for /users or /messages:

{
  "rules": {
    "users": {
      // Rules for data under /users
    },
    "messages": {
      // Rules for data under /messages
    }
  }
}

Basic Read Permissions

The ".read" rule determines who can retrieve data from a specific path. If a read request matches a path with ".read": "true", it's allowed. If it's "false", it's denied.

Here's how you might set read permissions:

{
  "rules": {
    "publicPosts": {
      ".read": "true" // Anyone can read blog posts
    },
    "secretDocs": {
      ".read": "false" // No one can read secret documents
    }
  }
}

Basic Write Permissions

Similarly, the ".write" rule controls who can create, update, or delete data at a given path. Setting it to "true" allows writes, and "false" denies them.

Let's look at some write rule examples:

{
  "rules": {
    "guestbook": {
      ".write": "true" // Anyone can sign the guestbook
    },
    "adminSettings": {
      ".write": "false" // No one can change admin settings yet
    }
  }
}

Combining Read & Write

You can define both ".read" and ".write" rules for the same path. Firebase evaluates them independently.

For example, to make a path readable by everyone but writable by no one (yet):

{
  "rules": {
    "announcements": {
      ".read": "true",  // Everyone can see announcements
      ".write": "false" // No one can post new announcements
    }
  }
}

Dynamic Paths with Wildcards

What if you have many items under a path, like individual user profiles (/users/user123, /users/user456)? You don't want to write a rule for each one!

Use a wildcard variable, prefixed with $, to match any child node. This variable can then be used within the rule itself.

{
  "rules": {
    "profileData": {
      "$userId": {
        ".read": "true",  // Anyone can read any user's profile
        ".write": "false" // But no one can edit them yet
      }
    }
  }
}

`auth` & `data`: Rule Helpers

When writing more advanced rules, you'll often need to check who is making the request or what data already exists. Firebase provides special variables for this:

  • auth: Contains information about the currently authenticated user (if any).
  • data: Refers to the data that already exists at the path being accessed.
  • newData: Refers to the data being written (only for write/validate rules).

These let you create smart rules, like "only the owner can edit their profile." We'll dive into these in upcoming lessons!

Syntax Check

Given the Firebase Realtime Database Security Rules below, which statement is true?

{
  "rules": {
    "posts": {
      ".read": "true",
      "comments": {
        ".write": "false"
      }
    },
    "users": {
      "$userId": {
        ".read": "true"
      }
    }
  }
}

Lesson Summary

Great job! In this lesson, we covered the foundational syntax of Firebase Realtime Database Security Rules:

  • Rules live in a rules.json file.
  • Rules are nested to target specific data paths.
  • ".read" and ".write" control read and write access.
  • Wildcards ($variable) make rules dynamic for child nodes.
  • You got a sneak peek at context variables like auth and data.

Next, we'll dive deeper into using these rules for user-based access control!

Często zadawane pytania

Czy lekcja „Zrozumienie składni reguł bezpieczeństwa” jest bezpłatna?

Tak — pełny tekst „Zrozumienie składni reguł bezpieczeństwa” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Firebase Auth & Realtime Database Apps, przejdź na CoddyKit PRO. Kurs Firebase Auth & Realtime Database Apps zawiera 4 lekcji w sumie.

Co nauczysz się w „Zrozumienie składni reguł bezpieczeństwa”?

Poznaj składnię i strukturę Firebase Realtime Database Security Rules służących do definiowania uprawnień dostępu. Ćwiczysz Firebase Auth & Realtime Database Apps z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Firebase Auth & Realtime Database Apps?

Nie wymagamy żadnego doświadczenia. Firebase Auth & Realtime Database Apps w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.

Ile czasu zajmuje lekcja „Zrozumienie składni reguł bezpieczeństwa”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Firebase Auth & Realtime Database Apps?

Tak. Każda lekcja Firebase Auth & Realtime Database Apps zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Zrozumienie składni reguł bezpieczeństwa
  2. Kontrola dostępu oparta na użytkownikach
  3. Walidacja danych za pomocą reguł
  4. Testowanie i debugowanie reguł bezpieczeństwa
← Powrót do Firebase Auth & Realtime Database Apps