Firebase的云功能:获取数据库的实际时间戳 [英] Cloud Functions for Firebase: get actual timestamp of database

查看:56
本文介绍了Firebase的云功能:获取数据库的实际时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取Firebase数据库服务器的实际时间戳,而不是占位符firebase.database.ServerValue.TIMESTAMP.

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

为了避免XY问题,我将提供两个用例:

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

a)我已经使用云功能和JWT创建了自己的重置密码系统.用户要求重设密码.我签署了一个包含其用户名和当前时间戳的对象(当前使用Date.now()),并将其嵌入到其电子邮件的链接中.当他们打开链接时,我需要验证JWT,并将其与存储在Firebase数据库中uid下的时间戳进行比较.我还将更新数据库中的时间戳记,以便下次他们尝试使用相同的令牌时,它将显示为已过期(即使我为令牌设置的1小时过期未通过).在这里使用Date.now()是否足够?如何将当前Firebase时间传递给jwt.sign().

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)为了排序目的,我需要在数据库中存储所有帖子的creationTimestamp的否定版本. -firebase.database.ServerValue.TIMESTAMP不起作用.正确的方法是什么?

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?

推荐答案

Google经历了严重麻烦确保所有服务器的时间都尽可能紧密地同步.因此,如果您正在Cloud Functions中运行代码(该代码最终在Google服务器上运行),并且想知道另一台Google服务器(例如实时数据库)上的时间,只需使用Date.now().在Cloud Functions中运行时,无需使用特殊的数据库令牌作为时间戳.

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.

这篇关于Firebase的云功能:获取数据库的实际时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆