Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow Read access to one specific collection in Firebase Firestore

On my android app, I am using Firebase Firestore to store a collection of "usernames" to be queried BEFORE the user signs in to check whether the chosen username is available or not.

Currently my project has 2 main collections: "users" and "usernames". I am trying to allow Read access only in the "usernames" collection. My program fails because when I try to query the "usernames" collection before the user is authenticated it says I don't have the right permissions.

My rules are very basic, but I was wondering if it is possible to modify them in some way for my project to work. Rules are as follows:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

A visual idea what the firestore database looks like:

enter image description here

like image 794
Apollocy Avatar asked Oct 27 '25 02:10

Apollocy


1 Answers

If you want to treat the "usernames" collection differently than everything else, call it out by name, and assign full read access to everyone:

    match /usernames/{id} {
      allow read: if true;
    }

I suggest taking some time to study the documentation on security rules to better understand how they work, an learn how to make rules on your own.

like image 92
Doug Stevenson Avatar answered Oct 28 '25 20:10

Doug Stevenson



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!