2016-10-28 38 views
2


我目前正試圖通過檢查最後發佈的時間戳來實現速率限制,然後向其添加+ 60秒,然後檢查它是否更小(<),然後將當前Firebase服務器時間戳(現在)。 它總是返回true並授予訪問權限!

這是我的規則:實時數據庫規則中的Firebase速率限制

{ 
    "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" 
     } 
    } 
} 
+3

實際上,您需要添加60000,因爲它計數毫秒 –

+0

聽起來像一個答案@VladimirGabrielyan! :-) –

回答

0

感謝您的線索弗拉基米爾!

正如我還沒有發現這種soulution anyware的我想在這裏正式分享答案:

{ 
    "rules": { 
     "posts": { 
     ".read": true, 
      ".write": 
"(root.child('users').child(auth.uid).child('lastPost').val() + 60000) < now" 
     } 
    } 
} 

說明:

當用戶帖子的東西,你總是更新值在值爲firebase.database.ServerValue.TIMESTAMP的數據庫中添加用戶信息。 在您閱讀的Rule語言中,讀取最後發佈的時間戳是從想要發佈的用戶(FB規則語言中的auth.uid)中讀出並添加60秒(* 1000,因爲Firebase在其時間戳中使用毫秒),這將是用戶被允許再次發佈的時間。然後檢查當前服務器時間戳是否高於允許用戶再次發佈的時間(<)。

希望它幫助你們,快樂編碼 - 做3天的Firebase,這太棒了!