Firestore Rules verify timestamp with a Flutter client
You can use the Timestamp
to add constraints to the time field (docs).
Here is an example of how to ensure that the change was within a certain amount of seconds:
function withinSeconds(secs) {
return request.resource.data.TIMEFIELD.seconds() - request.time.seconds() <= secs
&& request.resource.data.TIMEFIELD.seconds() - request.time.seconds() >= -secs
}
Edit
The above is for setting the value within a threshold of the request.time
.
You can also just use the REST API in the mean time. Just make a write
request that includes an update
and a transform
. The transform
is where you would set the server timestamp. Here is a tool to help understand how to build the requests.
This has been implemented into the Flutter plugin for Cloud Firestore:
FieldValue.serverTimestamp()
Using this as a field's value will assign a timestamp equal to request.time
to the field, server-side.
You can find out more about it in the API reference for cloud_firestore
.