Search code examples
firebasegoogle-cloud-firestorefirebase-security

firestore security-rule syntax: check if document name contains xyz


I have a document whose name consists of two user keys, can I check whether the key of the querying user is included.

for example the user fvmAPXO4BYUVJYkeSxsgsgzjjs should have access to the document 2B0ABrxKgjkrefjCP8tuMgq12e4-fvmAPXO4BYUVJYkeSxsgsgzjjs

I need something like: allow read, write: if document name contains ("request.auth.uid") ;


Solution

  • You can use matches() as shown below:

    match /collection/{docId} {
      allow read: if docId.matches(".*"+request.auth.uid+".*");
    }
    

    This regular expression will simply check if request.auth.uid is present in the document ID.