Firebase Rate Limiting In Realtime Database Rules Language

前端 未结 1 1310
轻奢々
轻奢々 2021-01-15 05:32


I am currently trying to implement rate limting by checking the timestamp of the last post and then add + 60sec to it and then check if it is smaller(<) then the cur

相关标签:
1条回答
  • 2021-01-15 06:20

    Thanks for your Clue Vladimir!

    As I haven't found this kind of soulution anyware I would like to share the answer here officially:

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

    Explanation:

    When a user posts something you always update the Value in the Database with the Value of firebase.database.ServerValue.TIMESTAMP to the user information. In the Rule language you read the Timestamp of the Last Post is read out of the user who wants to post (auth.uid in FB Rule Language) and add 60 seconds (*1000 as Firebase uses Milliseconds in it's timestamp), which would be the time when the user would be allowed to post again. And Then check if the current server timestamp is higher (<) than the time the user is allowed to post again.

    Hope It helped you guys, Happy Coding - Doing Firebase for 3 days and it's great!

    0 讨论(0)
提交回复
热议问题