Firestore Security Rule:Add data based on timestamp value
up vote
1
down vote
favorite
I want to read or write some data from/to firestore based on a security rule that enforces user to correct his local machine date should be correct ,means neither past nor future.If the client machine time is not correct, he does not have the ability to read or write data;
now i have tried with security rule
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read:if true;
allow create: if (request.resource.data.timestamp == request.time.toMillis());
}
}
}
but it always shows a message that permission denied.How can achieve this.?
my code is
batch.set(ref, {
timestamp: new Date().valueOf(),
name: formData.name ? formData.name.toLowerCase() : null,
type: formData.type ? formData.type : 'percentage',
rate: isNaN(formData.rate) ? 0 : Number(formData.rate),
date1: formData.date1 ? new Date(formData.date1) : null,
date2: formData.date2 ? new Date(formData.date2) : null,
time1: formData.time1 ? formData.time1 : null,
time2: formData.time2 ? formData.time2 : null,
id: ref.id,
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
typeArray,
enabledDays: dateArray,
isActive: true,
isTotalEnabled: formData.dOnTotal,
});
Thanks in advance
google-cloud-firestore angular6 firebase-security-rules
add a comment |
up vote
1
down vote
favorite
I want to read or write some data from/to firestore based on a security rule that enforces user to correct his local machine date should be correct ,means neither past nor future.If the client machine time is not correct, he does not have the ability to read or write data;
now i have tried with security rule
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read:if true;
allow create: if (request.resource.data.timestamp == request.time.toMillis());
}
}
}
but it always shows a message that permission denied.How can achieve this.?
my code is
batch.set(ref, {
timestamp: new Date().valueOf(),
name: formData.name ? formData.name.toLowerCase() : null,
type: formData.type ? formData.type : 'percentage',
rate: isNaN(formData.rate) ? 0 : Number(formData.rate),
date1: formData.date1 ? new Date(formData.date1) : null,
date2: formData.date2 ? new Date(formData.date2) : null,
time1: formData.time1 ? formData.time1 : null,
time2: formData.time2 ? formData.time2 : null,
id: ref.id,
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
typeArray,
enabledDays: dateArray,
isActive: true,
isTotalEnabled: formData.dOnTotal,
});
Thanks in advance
google-cloud-firestore angular6 firebase-security-rules
request.resource.data.timestamp == request.time.toMillis()
. Are you expecting both timestamps to match down to a millisecond? Don't think it's possible (network delay, code execution delay...), you need to give it a bit more breathing space (like 10 - 15 seconds maybe).
– MrAleister
17 hours ago
How can i give a breathing space? can you please explain it? Or is there any way to check whether the client date is today date or not using firestore security rule?
– pepe
17 hours ago
Please see my answer. How precise you need to be (seconds, minutes, hours) ? Do you care about time zones ?
– MrAleister
17 hours ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to read or write some data from/to firestore based on a security rule that enforces user to correct his local machine date should be correct ,means neither past nor future.If the client machine time is not correct, he does not have the ability to read or write data;
now i have tried with security rule
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read:if true;
allow create: if (request.resource.data.timestamp == request.time.toMillis());
}
}
}
but it always shows a message that permission denied.How can achieve this.?
my code is
batch.set(ref, {
timestamp: new Date().valueOf(),
name: formData.name ? formData.name.toLowerCase() : null,
type: formData.type ? formData.type : 'percentage',
rate: isNaN(formData.rate) ? 0 : Number(formData.rate),
date1: formData.date1 ? new Date(formData.date1) : null,
date2: formData.date2 ? new Date(formData.date2) : null,
time1: formData.time1 ? formData.time1 : null,
time2: formData.time2 ? formData.time2 : null,
id: ref.id,
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
typeArray,
enabledDays: dateArray,
isActive: true,
isTotalEnabled: formData.dOnTotal,
});
Thanks in advance
google-cloud-firestore angular6 firebase-security-rules
I want to read or write some data from/to firestore based on a security rule that enforces user to correct his local machine date should be correct ,means neither past nor future.If the client machine time is not correct, he does not have the ability to read or write data;
now i have tried with security rule
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read:if true;
allow create: if (request.resource.data.timestamp == request.time.toMillis());
}
}
}
but it always shows a message that permission denied.How can achieve this.?
my code is
batch.set(ref, {
timestamp: new Date().valueOf(),
name: formData.name ? formData.name.toLowerCase() : null,
type: formData.type ? formData.type : 'percentage',
rate: isNaN(formData.rate) ? 0 : Number(formData.rate),
date1: formData.date1 ? new Date(formData.date1) : null,
date2: formData.date2 ? new Date(formData.date2) : null,
time1: formData.time1 ? formData.time1 : null,
time2: formData.time2 ? formData.time2 : null,
id: ref.id,
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
typeArray,
enabledDays: dateArray,
isActive: true,
isTotalEnabled: formData.dOnTotal,
});
Thanks in advance
google-cloud-firestore angular6 firebase-security-rules
google-cloud-firestore angular6 firebase-security-rules
asked 17 hours ago
pepe
102310
102310
request.resource.data.timestamp == request.time.toMillis()
. Are you expecting both timestamps to match down to a millisecond? Don't think it's possible (network delay, code execution delay...), you need to give it a bit more breathing space (like 10 - 15 seconds maybe).
– MrAleister
17 hours ago
How can i give a breathing space? can you please explain it? Or is there any way to check whether the client date is today date or not using firestore security rule?
– pepe
17 hours ago
Please see my answer. How precise you need to be (seconds, minutes, hours) ? Do you care about time zones ?
– MrAleister
17 hours ago
add a comment |
request.resource.data.timestamp == request.time.toMillis()
. Are you expecting both timestamps to match down to a millisecond? Don't think it's possible (network delay, code execution delay...), you need to give it a bit more breathing space (like 10 - 15 seconds maybe).
– MrAleister
17 hours ago
How can i give a breathing space? can you please explain it? Or is there any way to check whether the client date is today date or not using firestore security rule?
– pepe
17 hours ago
Please see my answer. How precise you need to be (seconds, minutes, hours) ? Do you care about time zones ?
– MrAleister
17 hours ago
request.resource.data.timestamp == request.time.toMillis()
. Are you expecting both timestamps to match down to a millisecond? Don't think it's possible (network delay, code execution delay...), you need to give it a bit more breathing space (like 10 - 15 seconds maybe).– MrAleister
17 hours ago
request.resource.data.timestamp == request.time.toMillis()
. Are you expecting both timestamps to match down to a millisecond? Don't think it's possible (network delay, code execution delay...), you need to give it a bit more breathing space (like 10 - 15 seconds maybe).– MrAleister
17 hours ago
How can i give a breathing space? can you please explain it? Or is there any way to check whether the client date is today date or not using firestore security rule?
– pepe
17 hours ago
How can i give a breathing space? can you please explain it? Or is there any way to check whether the client date is today date or not using firestore security rule?
– pepe
17 hours ago
Please see my answer. How precise you need to be (seconds, minutes, hours) ? Do you care about time zones ?
– MrAleister
17 hours ago
Please see my answer. How precise you need to be (seconds, minutes, hours) ? Do you care about time zones ?
– MrAleister
17 hours ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
request.resource.data.timestamp == request.time.toMillis()
. Give it a bit more leeway (10 s for example):
math.abs(request.resource.data.timestamp - request.time.toMillis()) < 10000;
New contributor
thanks for your answer.if the leeway time is more than that of we mentioned,we can't make any read/write operation.How can we overcome this scenario?
– pepe
17 hours ago
I'm afraid I don't understand. Judging by the example of rules you gave, you dont want to allow write operation if record timestamp does not match server time. My example calculates absolute difference between two timestamps (in milliseconds) and check if its smaller than some leeway const. If not - there will be no write allowed (permission denied error). Please be aware this is not taking into account timezone differences (as I assume your record is created by client browser code - not by cloud function)
– MrAleister
17 hours ago
i applied your answer it perfectly works and i got your points.But it is my curiosity to know that is there any better way rather than this.?
– pepe
17 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
request.resource.data.timestamp == request.time.toMillis()
. Give it a bit more leeway (10 s for example):
math.abs(request.resource.data.timestamp - request.time.toMillis()) < 10000;
New contributor
thanks for your answer.if the leeway time is more than that of we mentioned,we can't make any read/write operation.How can we overcome this scenario?
– pepe
17 hours ago
I'm afraid I don't understand. Judging by the example of rules you gave, you dont want to allow write operation if record timestamp does not match server time. My example calculates absolute difference between two timestamps (in milliseconds) and check if its smaller than some leeway const. If not - there will be no write allowed (permission denied error). Please be aware this is not taking into account timezone differences (as I assume your record is created by client browser code - not by cloud function)
– MrAleister
17 hours ago
i applied your answer it perfectly works and i got your points.But it is my curiosity to know that is there any better way rather than this.?
– pepe
17 hours ago
add a comment |
up vote
1
down vote
accepted
request.resource.data.timestamp == request.time.toMillis()
. Give it a bit more leeway (10 s for example):
math.abs(request.resource.data.timestamp - request.time.toMillis()) < 10000;
New contributor
thanks for your answer.if the leeway time is more than that of we mentioned,we can't make any read/write operation.How can we overcome this scenario?
– pepe
17 hours ago
I'm afraid I don't understand. Judging by the example of rules you gave, you dont want to allow write operation if record timestamp does not match server time. My example calculates absolute difference between two timestamps (in milliseconds) and check if its smaller than some leeway const. If not - there will be no write allowed (permission denied error). Please be aware this is not taking into account timezone differences (as I assume your record is created by client browser code - not by cloud function)
– MrAleister
17 hours ago
i applied your answer it perfectly works and i got your points.But it is my curiosity to know that is there any better way rather than this.?
– pepe
17 hours ago
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
request.resource.data.timestamp == request.time.toMillis()
. Give it a bit more leeway (10 s for example):
math.abs(request.resource.data.timestamp - request.time.toMillis()) < 10000;
New contributor
request.resource.data.timestamp == request.time.toMillis()
. Give it a bit more leeway (10 s for example):
math.abs(request.resource.data.timestamp - request.time.toMillis()) < 10000;
New contributor
New contributor
answered 17 hours ago
MrAleister
24419
24419
New contributor
New contributor
thanks for your answer.if the leeway time is more than that of we mentioned,we can't make any read/write operation.How can we overcome this scenario?
– pepe
17 hours ago
I'm afraid I don't understand. Judging by the example of rules you gave, you dont want to allow write operation if record timestamp does not match server time. My example calculates absolute difference between two timestamps (in milliseconds) and check if its smaller than some leeway const. If not - there will be no write allowed (permission denied error). Please be aware this is not taking into account timezone differences (as I assume your record is created by client browser code - not by cloud function)
– MrAleister
17 hours ago
i applied your answer it perfectly works and i got your points.But it is my curiosity to know that is there any better way rather than this.?
– pepe
17 hours ago
add a comment |
thanks for your answer.if the leeway time is more than that of we mentioned,we can't make any read/write operation.How can we overcome this scenario?
– pepe
17 hours ago
I'm afraid I don't understand. Judging by the example of rules you gave, you dont want to allow write operation if record timestamp does not match server time. My example calculates absolute difference between two timestamps (in milliseconds) and check if its smaller than some leeway const. If not - there will be no write allowed (permission denied error). Please be aware this is not taking into account timezone differences (as I assume your record is created by client browser code - not by cloud function)
– MrAleister
17 hours ago
i applied your answer it perfectly works and i got your points.But it is my curiosity to know that is there any better way rather than this.?
– pepe
17 hours ago
thanks for your answer.if the leeway time is more than that of we mentioned,we can't make any read/write operation.How can we overcome this scenario?
– pepe
17 hours ago
thanks for your answer.if the leeway time is more than that of we mentioned,we can't make any read/write operation.How can we overcome this scenario?
– pepe
17 hours ago
I'm afraid I don't understand. Judging by the example of rules you gave, you dont want to allow write operation if record timestamp does not match server time. My example calculates absolute difference between two timestamps (in milliseconds) and check if its smaller than some leeway const. If not - there will be no write allowed (permission denied error). Please be aware this is not taking into account timezone differences (as I assume your record is created by client browser code - not by cloud function)
– MrAleister
17 hours ago
I'm afraid I don't understand. Judging by the example of rules you gave, you dont want to allow write operation if record timestamp does not match server time. My example calculates absolute difference between two timestamps (in milliseconds) and check if its smaller than some leeway const. If not - there will be no write allowed (permission denied error). Please be aware this is not taking into account timezone differences (as I assume your record is created by client browser code - not by cloud function)
– MrAleister
17 hours ago
i applied your answer it perfectly works and i got your points.But it is my curiosity to know that is there any better way rather than this.?
– pepe
17 hours ago
i applied your answer it perfectly works and i got your points.But it is my curiosity to know that is there any better way rather than this.?
– pepe
17 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371182%2ffirestore-security-ruleadd-data-based-on-timestamp-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
request.resource.data.timestamp == request.time.toMillis()
. Are you expecting both timestamps to match down to a millisecond? Don't think it's possible (network delay, code execution delay...), you need to give it a bit more breathing space (like 10 - 15 seconds maybe).– MrAleister
17 hours ago
How can i give a breathing space? can you please explain it? Or is there any way to check whether the client date is today date or not using firestore security rule?
– pepe
17 hours ago
Please see my answer. How precise you need to be (seconds, minutes, hours) ? Do you care about time zones ?
– MrAleister
17 hours ago