0

This is first time i am working with Firebase on Android. I am trying to insert unique username in firebase database. That means if the username is already exist then it will not be inserted. My database structure is users->uid->uid:value, username:value .

My database structure is given here -

{   "users" : {
    "23fcdc28-3d98-4f9a-804f-63e221d78d67" : {
      "uid" : "23fcdc28-3d98-4f9a-804f-63e221d78d67",
      "username" : "bbb"
    },
    "2d72161c-6164-45ea-b1fc-0965b838bf08" : {
      "uid" : "2d72161c-6164-45ea-b1fc-0965b838bf08",
      "username" : "tazimete"
    },
    "751d5c88-ce14-4931-9c79-4eae387878bb" : {
      "uid" : "751d5c88-ce14-4931-9c79-4eae387878bb",
      "username" : "aaa"
    }   } }

And the rules for the database structure is given here

{
    "rules": {
        ".read": true,
        ".write": true,
        "users": {
          "$uid": {
            ".read": true,
            ".write": true,
            "$username": {
                ".read": true,
                ".write": true,
                ".validate": "root.child('users').child($uid).child($username).val() !== newData.child('username').val()"
            }
          }
      }    } }

But its not working. I am trying to solve it by putting rules on admin panel. but if you have any other idea, please share with me with code. Thanks.

AGM Tazim
  • 2,213
  • 3
  • 16
  • 25
  • You've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export button in your Firebase database. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. Same for the security rules. – Frank van Puffelen Jun 08 '16 at 13:26
  • @Frank van Puffelen. Thanks for your comment. Now i have replaced image with code. Can you please look at it again? – AGM Tazim Jun 09 '16 at 07:29
  • See http://stackoverflow.com/questions/25294478/how-do-you-prevent-duplicate-user-properties-in-firebase – Frank van Puffelen Jun 27 '16 at 17:18

0 Answers0