Cloud Functions for Firebase: get actual timestamp of database

房东的猫 提交于 2019-12-25 09:35:03

问题


I need to get the actual timestamp of the firebase database servers instead of the placeholder firebase.database.ServerValue.TIMESTAMP.

In order to avoid an XY problem, I will provide my two use cases:

a) I have created my own reset password system using cloud functions and JWT. A user requests to reset their password. I sign an object containing their username and the current timestamp(currently using Date.now()) and send it embedded to a link to their email. When they open the link, I need to verify the JWT and also compare it to a timestamp stored under their uid in firebase database. I also update the timestamp in the database so next time they try to use the same token, it will show up as expired(even if the 1 hour expiration I have set for the token has not passed). Is using Date.now() here enough? How can I pass the current firebase time to jwt.sign().

b) I need to store a negative version of the creationTimestamp of all posts in the database for sorting purposes. -firebase.database.ServerValue.TIMESTAMP does not work. What is the correct way to do that?


回答1:


Google goes through great trouble to make sure all of their servers have times that are as closely synchronized as possible. So, if you're running code in Cloud Functions, which ends up on a Google server, and you want to know the time on another Google server (like your Realtime Database), just use Date.now(). There's no need to use the special database token for timestamp when running in Cloud Functions.

That timestamp token is intended to be useful when code is running on client devices whose clocks may not be very well synchronized.



来源:https://stackoverflow.com/questions/46206593/cloud-functions-for-firebase-get-actual-timestamp-of-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!