pedalpete
pedalpete

Reputation: 21536

Firestore data with multiple variables in document name

I'm creating an app where users have their own calendars, and those calendars have events.

I've created a firestore document called 'days', my intention was to have the structure of

- days
  - {user1}-{date}
     - events: [{array of events during the day}]
  - {user2-{date}
...

In the rules, only the owner should be able to view their event data.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId}/{documents=**} {
      allow read, write: if isOwner(userId)
    }
     match /day/{userId}-{date}/{documents=**} {
      allow read, write: if isOwner(userId)
    }
  }

  function isOwner(userId) {
    return request.auth.uid == userId;
  }
}

I've tried multiple structures of creating a regex to get {userId}-{date} but haven't got anything that is working.

Is this possible with Firebase? Am I doing this structurally wrong? Should I be using sub-collections?

Upvotes: 0

Views: 82

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317412

Firestore security rules don't allow any type of regex on the path of a document. A rule can only match 0 or more entire path segments using a wildcard. You will need to find a different way to structure your data.

The names of collections are not themselves meant to contain multiple items of data. In fact, it's better if they don't contain any data at all (excepting things like UID, whose specific values need to be used in security rules). I suggest simply moving the date to a field in the document for the day collection, or using a subcollection to group things under a parent document.

Upvotes: 1

Related Questions