我目前正在尝试通过检查上一个帖子的时间戳,然后向其添加+ 60秒,然后检查它是否小于当前Firebase Server时间戳(现在),来实现速率限制。它总是以某种方式返回true并授予访问权限?!
这些是我的规则:
{
"rules": {
"posts": {
".read": true,
".write":
"(root.child('users').child(auth.uid).child('lastPost').val() + 60) < now"
}
}
}
这是我的数据库结构
{
"posts": {
"-KV70ppGGTEtXY4_Q4UC": {
"author": "abcdedef-uid-ojifgoöifjgssgd",
"description": "Thats the post description",
"title": "Thats the post title"
}
},
"users": {
"2uy7323nTodMHcVxeEDJzoexH302": {
"canPost": true,
"email": "[email protected]",
"lastPost": 14776667681,
"profile_picture": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg",
"username": "Cryptic Pug"
}
}
}
感谢您的线索弗拉基米尔!
因为我还没有找到这种解决方案的任何软件,所以我想在这里正式分享答案:
{
"rules": {
"posts": {
".read": true,
".write":
"(root.child('users').child(auth.uid).child('lastPost').val() + 60000) < now"
}
}
}
解释:
当用户发布内容时,您总是使用“用户的值”更新数据库中的firebase.database.ServerValue.TIMESTAMP
“用户”信息。在“规则语言”中,您读取了要发布的用户的“上次发布的时间戳”(auth.uid
使用FB规则语言),并添加了60秒(* 1000,因为Firebase在其时间戳中使用了毫秒),用户将被允许再次发布。然后检查当前服务器的时间戳是否高于(<)大于允许用户再次发布的时间。
希望它对你们有帮助,Happy Coding-做Firebase 3天了,太好了!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句