0

I want to add users to database with unique usernames. The problem is that user with the same username gets created but without username field. here's my rules :

   "users": {
    "$uid": {
      ".write": "auth !== null && auth.uid === $uid ",
      ".read": "auth !== null && auth.provider === 'password'",
        "Username": {
        ".validate": "
          !root.child('usernames').child(newData.val()).exists() ||
          root.child('usernames').child(newData.val()).val() == $uid"
      }
    }
   },
   "usernames": {
      ".read": true,
      ".write": true,
   }

I store unique usernames in "usernames" table

enter image description here

I don't want upper node to be created in users table. How do i change rules to achieve that?

Drew Szurko
  • 1,601
  • 1
  • 16
  • 32
  • does it have to be in firebase, I mean an sqlDB can easily remove the headache – user7568042 Feb 25 '17 at 20:36
  • yes it has to be on firebase – Mindaugas Brazionis Feb 25 '17 at 20:49
  • If that's your `usernames` node, one of your users doesn't have a username. So that will not work. Aside from that: ensuring unique usernames has been covered quite a bit already. So unless you share the code where you are stuck, I'd recommend first reading some of these existing questions and answers: http://stackoverflow.com/search?tab=votes&q=%5bfirebase%5d%20unique%20username%20is%3aanswer – Frank van Puffelen Feb 25 '17 at 21:08
  • I'm storing usernames both in users and usernames table, I just need some kind of validation to deny writing to users table if there is a same value in usernames table – Mindaugas Brazionis Feb 25 '17 at 21:17
  • With your current structure that is not possible. You cannot ensure that a *value* is unique with security rules. So you'll need to store the usernames as keys, not as values. The top two answer in the list I provided explain precisely that. – Frank van Puffelen Feb 25 '17 at 21:25

0 Answers0