Search code examples
firebasegoogle-cloud-firestorefirebase-security

Can I add Firestore rules for each collection I have?


So, for now im just trying to apply different rules to each one of the collections I have on my database but everytime i've tried this, it just rejects every operation no matter what, but accepts the operations if the rule is general for the whole database

    rules_version = '2';
    service cloud.firestore {
        match /databases/{database}/documents{
            match /users/{user} {
                allow read, write, update, delete: if 
                    request.auth !=null
            }
            match /productos/{product} {
                allow read;
                allow write, update, delete:
                    if request.auth !=null
                }
        
            match /business/{business} {
                allow read;
                allow write, update, delete: if 
                    request.auth !=null
            }
            match /orders/{order} {
                allow write, update, delete: if 
                    request.auth !=null
                allow read;
            }
        }
    }

    match /business/{business} {
        allow read;
        allow write, update, delete: if 
            request.auth !=null
    }
    match /orders/{order} {
        allow write, update, delete: if 
            request.auth !=null
        allow read;
    }
  }
}

this is what I'm trying and what I've seen on the internet but it just doesn't work


Solution

  • Matches are hierarchical, so they all need to be under match /databases/{database}/documents{ to match anything in your database. So the last two match statements in your rules won't work unless you prefix the path with /databases/{database}/documents.