admin管理员组文章数量:1323029
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
2 Answers
Reset to default 8Here 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
本文标签:
版权声明:本文标题:javascript - What is the best way to get Server Timestamp, using Firebase Cloud Firestore, Ionic Framework, Angular? - Stack Ove 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742107794a2421102.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论