简体   繁体   中英

Firebase Realtime Database - Allow users to delete their own data

I currently have the following rules on my database which basically (a) allow users to enter their data if they have not done it yet ( this is for their first time) -->.data.exists (b) allow them to write only if their new values are less than the current ones. --> data.child('time').val() > newData.child('time').val() and finally (c) allow them to change their username --> data.child('userName').val().= newData.child('userName').val()"

All of the above work perfectly but now due to also Google PLay store policies i want the user to have the abillity to delete their data if they want to.

I tried several solutions like data.exists() but even though this works bypasses my other rule about the value check if it less and allows to write values that are greater than the existing one.

Can you help on this how to add another rule that will allow the user to delete their entry more specific the userID and at the same time all of the above continue working

My command is

var DBTaskWrite = DBReference.Child("Users").Child("3X3").Child(_userID).RemoveValueAsync();
{
  "rules": {            
    //".read": true,
      "Users" : {
           "3X3": {
              ".indexOn": ["time"],
             ".read": "data.exists()",
             "$userID": {
               ".write": "!data.exists()  || data.child('time').val() > newData.child('time').val() || data.child('userName').val() != newData.child('userName').val()"
             }
          }
       }
    }
  }

Tried the data.exists which is working, but bypass the other rules.

If you also want to allow the user to delete their data, you can add ||.newData.exists() to the rule. So:

"$userID": {
  ".write": "!data.exists() || !newData.exists || ..."
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM