creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126824

How to work with Timestamps in Firestore Security Rules

Imagine a value of type timestamp stored in a document.

allow update: if resource.data.customTimestamp.toMillis() == request.time.toMillis()

The above code does not work and throws the following error:

Function not found error: Name: [toMillis].

If I turn that around like this:

allow update: if request.time.toMillis() == resource.data.customTimestamp.toMillis()

The data acces is denied:

Simulated data access denied

This is very confusing to me.

Question

How do I parse a Firestore timestamp to a Security Rules Timestamp?

Screenshot from Firebase Console

Test Project

Stored data Rules simulator

Upvotes: 4

Views: 1869

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317692

There are two problems here that are unrelated, but are essentially bugs in the Firstore rules simulator.

First, request.time isn't currently simulated. It's unfortunately that a rule that tries to evaluate this is immediately rejected, rather than giving you a message saying that it's not supported.

Second, the Timestamp object methods that should be available on Timestamp typed fields in a document are broken. Timestamp fields are currently being sent to the simulator as an "empty object" with no methods, so you can't make use of their values in the simulator.

For both cases, published rules should work OK.

Upvotes: 4

Ronnie Smith
Ronnie Smith

Reputation: 18595

See rules.timestamp.

rules.timestamp

Globally available timestamp functions. These functions are accessed using the timestamp. prefix.

value(epochMillis) returns rules.Timestamp

Make a timestamp from an epoch time in milliseconds.

So, wrap it in value( )

Upvotes: -2

Related Questions