Home > Net >  comparing request.time to value set using toMillis() in firestore rules?
comparing request.time to value set using toMillis() in firestore rules?

Time:05-17

I have the following code and I want to compare 2 values and set rules accordingly, but apparently there is no consistency between the two, at least the comparation is not working issue was I want to make all timestamps stored in ms.

// Data Filed set in cloud fuction.
const expiresIn = 3600; // test 1h   //172800  === 48h
const createdAt = admin.firestore.Timestamp.now().toDate();
createdAt.setSeconds(createdAt.getSeconds()   expiresIn);

premiumUntill: admin.firestore.Timestamp.fromDate(createdAt).toMillis()


// Rules
&& request.auth.token.premiumUntill > request.time
&& resource.data.premiumUntill > request.time

CodePudding user response:

request.time is a Timestamp object, not time in millis. It's not directly comparable to an integer.

If you want to use request.time in rules, you should store a Firestore Timestamp object in the document field for direct comparison. Or you can convert the rules Timestamp to millis first before comparing it to an integer.

  • Related