简体   繁体   English

使用 Firebase Cloud Firestore、Ionic Framework、Angular 获取服务器时间戳的最佳方法是什么?

[英]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()并在安全规则中对其进行验证。

Example:例子:

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.

相关问题 如何使用 Firestore 在 Cloud Functions for Firebase 中获取服务器时间戳? - How do I get the server timestamp in Cloud Functions for Firebase with Firestore? 在Firebase Cloud Firestore中对某个领域的孩子进行CRUD的最佳方法是什么? - What is the best way to CRUD children of a field in firebase cloud firestore? 将多个用户图像上传到 Firestore 集合的最佳方式是什么? 使用 Angular/Ionic 4 - What is the best way to upload multiple user images to a firestore collection? using Angular/Ionic 4 从 Cloud Firestore 在 MatDatepicker 中加载时间戳 | 火力基地 - Load timestamp in MatDatepicker from Cloud Firestore | Firebase 使用Cloud Firestore和Javascript按时间戳排序和获取元素 - Order and get element by timestamp using cloud Firestore and Javascript 将 firebase.initializeApp() 与 Cloud Firestore 一起使用后,有没有办法关闭连接? - Is there a way to close the connection after using firebase.initializeApp() with Cloud Firestore? 使用 Firebase 的服务器时间戳 - Server timestamp using firebase 在Angular中倒退时间的最佳方法是什么 - What is the best way to get time backwards in Angular 尝试使用键值(角度)从云 Firestore 获取值 - Trying to get values from cloud Firestore using keyvalue (Angular) Firebase Cloud Firestore 规则允许获取但不允许列出 - Firebase Cloud Firestore rule permits get but not list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM