Home > Software design >  firestore security-rule syntax: check if document name contains xyz
firestore security-rule syntax: check if document name contains xyz

Time:03-28

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") ;

CodePudding user response:

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.

  • Related