ThatBrianDude
ThatBrianDude

Reputation: 3190

Firebase rule: Enforce value to be Servervalue.TIMESTAMP

How can I make sure that a servervalue is written to firebase via firebase rules?

I am writing a message to the realtime database like so:

newMessageRef.set({
      id: newMessageRef.key,
      author: "Username",
      content: "Message",
      timestamp: firebase.database.ServerValue.TIMESTAMP
})

How can I make sure that the client doesnt modify my code and use a custom timestamp?

Upvotes: 4

Views: 572

Answers (1)

André Kool
André Kool

Reputation: 4978

For this you can use the now variable in Firebase security rules like this:

{
  "rules": {
    "Messages": {
      "$newMessageRef": {
        "timestamp": {
          ".validate": "newData.val() === now"
        }
      }
    }
  }
}

Upvotes: 3

Related Questions