Search code examples
firebasegoogle-cloud-firestorefirebase-security

Firestore rule if true or false


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?


Solution

  • 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