Adriano Resende
Adriano Resende

Reputation: 2619

In Firestore I can not create rule to avoid overwrite

I'm trying to avoid overwriting, what's the best way to make the rules?

This rule does not work

service cloud.firestore {
  match /databases/{database}/documents {
    match /audio/{title} {
      allow read;
      allow write: if request.resource.data.id != resource.data.id;
    }
  }
}

I tried this rule and it did not work request.resource.id != resource.id;

Upvotes: 1

Views: 487

Answers (1)

Dan McGrath
Dan McGrath

Reputation: 42038

Take a look at the Security Rules reference documentation. Specifically the section about the allow statement.

There are three operations for writes in Cloud Firestore: create, update, and delete. These correspond to the set(), add(), update(), remove(), and transaction() methods in the client libraries. For your convenience the write operation allows all of these.

// Writes are divided into create, update, and delete operations
allow create, update, delete: if <condition>;

// This is equivalent to using the write operation
allow write: if <condition>;

Here you'll see that allow write is a convenience shortcut for all 3 types of mutations. For your use case you'll want something like:

allow create: if <condition>;
allow update, delete: if false;

Upvotes: 2

Related Questions