Search code examples
firebasegoogle-cloud-firestorefirebase-security

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


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


Solution

  • 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.