aaabc
aaabc

Reputation: 51

prevent "delete and update" a child in firebase

I see that there is no way to set security rules as preventing "delete and update" for a child.

".write": "!data.exists() && newData.exists() && !newData.exists()"

thats not make sense.

Upvotes: 4

Views: 2609

Answers (1)

Jen Person
Jen Person

Reputation: 7546

For future reference, the Firebase console lets you test database security rules, so you can find out what works right there before you publish those rules. That being said, if I'm understanding your question correctly, you want to allow users to add to the node, but not delete or update. You'd be looking for something along the lines of:

{
  "rules": {
    ...

    "childNodeName": {
       ".write": "!data.exists()"
    }
  }
}

You shouldn't need those other two conditions. Not to mention, they will never resolve to true since those conditions cannot be met.

You can also use a wildcard if you need to add multiple children to a path but you don't want the user to modify those children once they've been added:

{
  "rules": {
    ...

    "childNodeName": {
       "$pushId": {
          ".write": "!data.exists()"
      }
    }
  }
}  

Upvotes: 7

Related Questions