0

I have been looking at the following post Firebase android : make username unique

I have done more or less the same as the answer. But i am missing something. Maybe someone here have better eyes than me and can point out why it is not working

My code

FirebaseUser user = firebaseAuth.getCurrentUser();

  Map<String, Object> childUpdates = new HashMap<>();
  Map<String, Object> userValues = new HashMap<>();
  Map<String, Object> usernameValues = new HashMap<>();
  userValues.put("email", email);
  userValues.put("username", username);
  usernameValues.put(username, user.getUid());
  childUpdates.put("/Users/" + user.getUid() + "/", userValues);
  childUpdates.put("/Usernames/", usernameValues);


  mDatabaseReference.updateChildren(childUpdates).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
      hideProgressDialog();
      if(!task.isSuccessful()) {
        //Username taken. Force user in next view to pick one
      }
      Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(intent);
    }
  });

Snippet from the 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() == auth.uid"
    }
  }
}

In my head, I should add rules for Usernames as well. But i got really confused by the link above and the answer given. What should i then logically add to the Usernames ruleset

i am guessing that i need to do something like

"Usernames" : {
   "$uid": {
      ".read": "auth != null",
                ".write": "auth != null",
                ".validate" : "!root.child('Usernames').child($uid).exists()"
   }
}

Any help is thankful. Let me know if you want me to explain anything further

Community
  • 1
  • 1
Jemil Riahi
  • 1,360
  • 5
  • 18
  • 38

0 Answers0