最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the best way to get Server Timestamp, using Firebase Cloud Firestore, Ionic Framework, Angular? - Stack Ove

programmeradmin7浏览0评论

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.

If some of the user change the mobile time or system time, our plete 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.

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.

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

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.

If some of the user change the mobile time or system time, our plete 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.

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.

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

Share Improve this question edited Jan 16, 2019 at 6:11 PPS asked Jan 15, 2019 at 15:19 PPSPPS 5521 gold badge8 silver badges25 bronze badges 6
  • What did you try already? – Frank van Puffelen Commented Jan 15, 2019 at 15:41
  • If you do a search there are many services offering this functionality. Or you could create your own service to do this. – phuzi Commented Jan 15, 2019 at 15:42
  • It can be possible to get the server timestamp from firebase @phuzi – PPS Commented Jan 15, 2019 at 15:49
  • Like this? stackoverflow./questions/24202641/… – phuzi Commented Jan 15, 2019 at 15:50
  • 1 @phuzi: that question is for the Firebase Realtime Database, while this one is about Cloud Firestore. While both databases are part of Firebase, and both provide a way to set the server-side timestamp, the syntax is different. – Frank van Puffelen Commented Jan 15, 2019 at 15:54
 |  Show 1 more ment

2 Answers 2

Reset to default 8

Here a simple way to tell Firestore to write the server-side timestamp to a document:

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.

For a working sample of this code, see: https://jsbin./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.

Example:

To set serverTime in a document

      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

发布评论

评论列表(0)

  1. 暂无评论