[英]What is the best way to get Server Timestamp, using Firebase Cloud Firestore, Ionic Framework, Angular?
I am working in the project which truly based on clock time.我正在从事真正基于时钟时间的项目。 It has a functionality to work same on desktop and mobile app ( Android/iOS ) using Ionic Framework .
它具有使用Ionic Framework在桌面和移动应用程序( Android/iOS )上工作相同的功能。
If some of the user change the mobile time or system time, our complete app will get change the results.如果某些用户更改了移动时间或系统时间,我们完整的应用程序将得到更改结果。
So somewhere i found the some answer which shows that get the server time from web Server programming language.所以在某处我找到了一些答案,它表明从 web 服务器编程语言中获取服务器时间。
But i am not using any web framework language.但我没有使用任何网络框架语言。
triggerNextTicket(){
Observable.interval(1000).map((x) => {
let date = +new Date().getTime() ;
if( (this.nextTicket.ticket_live_at.seconds * 1000) < date){
this.ngOnInit();
}
if((this.ticketData.ticket_live_at.seconds * 1000) < date){
this.ngOnInit();
}
Actuaally, the main problem in this code is, if the Mobile system time changed by the user manually, then our app shows the past data .实际上,这段代码的主要问题是,如果用户手动更改了 Mobile 系统时间,那么我们的应用程序会显示过去的数据。
let date = +new Date().getTime()
in any case my date variable will be fresh, if the user change their system time or in any case.在任何情况下,如果用户更改系统时间或在任何情况下,我的日期变量都将是新鲜的。
Thanks谢谢
Here a simple way to tell Firestore to write the server-side timestamp to a document:这是告诉 Firestore 将服务器端时间戳写入文档的简单方法:
var ref = db.collection("54201787").doc("time");
ref.set({ timestamp: firebase.firestore.FieldValue.serverTimestamp() });
And here's how you read that value back into your client:以下是您将该值读回给客户的方法:
ref.onSnapshot(function(snapshot) {
var timestamp = snapshot.data().timestamp;
console.log(timestamp.toString());
})
In this last snippet the timestamp
variable is a Date
object , so you can access all its other methods and properties too.在最后一个片段中,
timestamp
变量是一个Date
对象,因此您也可以访问它的所有其他方法和属性。
For a working sample of this code, see: https://jsbin.com/burimih/edit?html,js,console有关此代码的工作示例,请参阅: https : //jsbin.com/burimih/edit?html,js,console
You should use firebase.firestore.FieldValue.serverTimestamp()
while creating/updating a document as well as validate the same in security rules.您应该在创建/更新文档时使用
firebase.firestore.FieldValue.serverTimestamp()
并在安全规则中对其进行验证。
To set serverTime in a document在文档中设置 serverTime
db.collection("fooBar")
.doc()
.set({
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
createdBy: this.props.user.uid,
foo: 'bar',
})
.then(() => console.log("Success"))
.catch(console.error);
Security Rule安全规则
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read;
allow write: if request.resource.data.createdBy == request.auth.uid && request.time == request.resource.data.createdAt
}
}
}
This way I can verify there are no client manipulated values in my database.这样我就可以验证我的数据库中没有客户端操纵的值。
Also, notice how I validate user uid against request.auth.uid
另外,请注意我如何根据
request.auth.uid
验证用户 uid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.