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

Only allow document to be read if ID is known

I have a collection named proposals, which I want to disable listing on it and allow only get if user know the ID.

Is it possible with Firebase?

My currently fails because "read" is allowed, but without this I can’t read the document.

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

match /proposals/{uid} {
  allow list, update, delete: if false;

  allow create, read: if true;
}

>Solution :

It’s pretty common to do this kind of authorization, and yes, it’s possible.
Firestore Security Rules provide to us request.auth.uid which contains the UID of the user making the request or null if it’s unauthenticated.

So, you could use that information with an equality operator:

match /proposals/{uid} {
  allow list, update, delete: if false;
  allow get: if request.auth.uid != null && request.auth.uid == uid;
  allow create;
}

You should use get to define rules that will apply when any user is trying to get a document. Note that read is for any type of read request, which includes get and list.

More about Security Rules and Authentication: https://firebase.google.com/docs/rules/rules-and-auth

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