Firestore rule if true or false

Viewed 2704

In firestore you can have rules like below:

service cloud.firestore {
  match /databases/{database}/documents {
    match /products/{document=**} {
        allow read, write: if true;
    }
}

What is meant by if true in this line: allow read, write: if true;?

What if it is if false?

2 Answers

It means, that read and write will evaluate to true here. which means that everyone can read and write your database.

if false --> will evaluate to the expression to false. for example:

allow read, write : if false

this will close your database and no one can modify it

It defines the required condition of whether the permissions apply to the path.

match <path> {
  allow <permissions>: if <condition>
}

The condition can simply be a true if you want permissions to apply without any requirements, or the condition can be result of one or multiple values ​​that evaluate to true or false.

match /chatroom/{roomId} {
  allow read, write: if (
    request.auth != null && 
    roomId in get(/users/$(request.auth.uid)).data.chats
  );
}

The documentation has better examples that explain it in more detail. https://firebase.google.com/docs/firestore/security/rules-conditions

Related