Apollocy
Apollocy

Reputation: 77

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

Upvotes: 4

Views: 5103

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317382

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.

Upvotes: 22

Related Questions