Felicia
Felicia

Reputation: 119

How to read/write specific data on Firestore

I'm having some problem with the READ rules of Firestore currently

Here is my data structure

{
  email: [email protected],
  username: geekGi3L,
  birthday: 1995/02/14,
  photo: <firestore-download-url>
}

The rules currently I set is

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{user} {
      allow read;
      allow write: if request.auth.uid != null && request.auth.uid == user;
    }
  }
}

How could I set the rules to allow user to READ the specific fields like email and birthday only if request.auth.uid != null && request.auth.uid == uid while username and photo should be readable by every user?

Thank you <3

Upvotes: 1

Views: 123

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317362

In Firstore, there is no per-field access control for reading fields of a document. The most granular unit of access is the document. A user either has full access to read a document in its entirety, or they don't have any access at all.

If you need to change access per field, you'll have to split the fields of the document into multiple collections, with each collection having access control appropriate for the fields of the documents within. It's very common to have a split between public and private data like this.

Upvotes: 2

Related Questions