Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how do you access a variable in a firebase document in the security rules?

I am trying to do firebase security rules in firestore, and I want to allow users to write data to the "posts" collection if the current users id (request.auth.uid is equal to the "uid" in my "posts" collection.

Here is a picture of my firebase collection. I want to access the "uid" variable in the "posts" collection.enter image description here

Here is what I have so far in my security rules:
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you in advance! Very much appreciated!

>Solution :

You can access the current (pre-write) data of the document through resource.data, and the updates (post-write if the write is allowed) as request.resource.data.

So to access your UID field, that’d be resource.data.uid or request.resource.data.uid respectively.

For more on this, see the Firebase documentation on data validation in security rules, on fixing open access rules, and on restricting updates to certain fields.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading