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

Firebase Security Rules: Allow if user uid exists in a collection as a document

I am collecting my admin users uid’s as documents in "admins" collection. I want to allow users to read if they have their uid’s in that collection as a document.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /admins/{document=**} {
      allow write; // Everyone should be able to write
      allow read: if request.auth.uid == ; // Only read if /admins/{youruid}
    }
  }
}

>Solution :

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

You can use exists() to check if a document exists. Try refactoring your rules as shown below:

match /admins/{document=**} {
  allow read: if exists(/databases/$(database)/documents/admins/$(request.auth.uid)); 
  // Only read if /admins/{youruid}
}

You can read more about exists() in the documentation.

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