Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase firestore security rules allow if document ID contains string

I have got the following:

match /direct/{postId} {
   allow read, write: if postId.includes(request.auth.uid);
}

However, I would like to only allow read, write if the document ID (postId) contains the string. .includes is not working for me in security rules.

Edit: it should match a substring instead of the entire document id

Edit2

screenshot of a db

collection called direct where each document ID is a string which contains 2 uids in it’s name.

Any help would be appreciated. Thanks.

like image 702
user812039 Avatar asked Sep 07 '26 20:09

user812039


1 Answers

The document ID is a string, and as far as I can see the String class in security rules doesn't have an include method.

It does have a matches method though, so you can use that to test whether the document ID contains a substring with a regular expression.

Something like:

match /direct/{postId} {
   allow read, write: if postId.matches(request.auth.uid);
}

A working read where the postId matches the UID of the user:

enter image description here

Trying to read another document:

enter image description here

Update: to test for a substring:

  allow read: if postId.matches(".*"+request.auth.uid+".*");
like image 119
Frank van Puffelen Avatar answered Sep 11 '26 20:09

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!