How do I check if user on client sided created document with only firebase.firestore.FieldValue.serverTimestamp()
?
I have following:
allow create: if request.resource.data.timestamp == ??
What should I have instead of ??
. I have tried serverTimestamp()
firebase.firestore.FieldValue.serverTimestamp()
, now
or now()
but it doesn't work.
It is possible to do it in Firebase like this:
".validate": "newData.child('timestamp').val() === now"
I am looking for the same solution. Any ideas? Thanks
firestore. Timestamp. A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one.
Cloud Firestore Security Rules allow you to control access to documents and collections in your database. The flexible rules syntax allows you to create rules that match anything, from all writes to the entire database to operations on a specific document.
firestore. FieldValue. Sentinel values that can be used when writing document fields with set() or update() .
You can access the current request timestamp in Security Rules using the request.time
attribute (docs), which is the Firestore equivalent to the Realtime Databases's now
. You'll therefore want something like:
allow create: if request.resource.data.timestamp == request.time;
For serverTimestamp()
this should evaluate to true.
You should always validate client input in Security Rules, even if you're using serverTimestamp()
. Security Rules doesn't automatically know the server input the value instead of the client, so without this check, a malicious client could create a different created at time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With