Firebase firestore timestamp based security rules

你离开我真会死。 提交于 2021-02-19 05:33:37

问题


On my client side I am creating a document with one of the fields "createdDate"

below is a payload from the firestore simulator

{
  "__name__": "/databases/(default)/documents/billing/aaaa",
  "data": {
    "createdDate": 1529237500239,
    "createdDateTimeFormat": "2018-06-12T07:00:00.000Z"
  }
}

I tried to set the security rule to make sure the "createdDate" is not in the future.

service cloud.firestore {
  match /databases/{database}/documents {
    match /billing/{userId}{
        allow create: if (request.resource.data.createdDate < request.time.toMillis());
    }     
  }
}

This gives access denied.

Next, I tried the following rule referencing the Timestamp format. That gives access denied as well.

service cloud.firestore {
  match /databases/{database}/documents {
    match /billing/{userId}{
        allow create: if (request.resource.data.createdDateTimeFormat < request.time);
    }     
  }
}

回答1:


request.time is of type Timestamp, as you can see from the rules reference documentation, but you're using it as a number.

You have two options. First, you could change your createdDate field to a Timestamp type object, which is probably the better way to go.

If you must leave it as a number, you'll have to convert the request.time Timestamp object to a number that matches the measurement of your number. If your number measured in milliseconds since epoch, you can use the toMills() method on it:

allow create: if (request.resource.data.createdDate < request.time.toMillis());


来源:https://stackoverflow.com/questions/50915616/firebase-firestore-timestamp-based-security-rules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!