title: Firebase - Forcing unique usernames
body: <p>Following information on similar questions on Stack I've ended up with the following:</p>
  
  <p>Database:</p>
  
  <pre><code>{
    "user" : {
      "xbStJyCtfzf472" : {
        "avatar" : "avatar",
        "email" : "email@gmail.com",
        "fbId" : 10154200898,
        "lastLogin" : "Tue Mar 14 22:31:36 GMT+01:00 2017",
        "registerDate" : "Tue Mar 14 22:31:36 GMT+01:00 2017",
        "uid" : "xbStJyCwY5Ttfzf472",
        "username" : "myName"
      }
    },
    "usernames" : {
      "myName" : "xbStJyCtfzf472"
    }
  }
  </code></pre>
  
  <p>Rules:</p>
  
  <pre><code>{
    "rules": {
      ".read": "auth != null",
      ".write": "auth != null",
  
      "users": {
      "$uid": {
          ".write": "auth !== null &amp;&amp; auth.uid === $uid",
          ".read": "auth !== null &amp;&amp; auth.provider === 'password'",
          "username": {
          ".validate": "
              !root.child('usernames').child(newData.val()).exists() ||
              root.child('usernames').child(newData.val()).val() == $uid"
              }
          }
          }
    }
  }
  </code></pre>
  
  <p>Android Java Code:</p>
  
  <pre><code>public void addUserToDatabase(final long fbId){
          final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
          if (user != null) {
              final String username = "myName";
  
              //add the user
              mDatabase.child("usernames").child(username).runTransaction(new Transaction.Handler() {
                  @Override
                  public Transaction.Result doTransaction(MutableData mutableData) {
                      if (mutableData.getValue() == null) {
                          mutableData.setValue(user.getUid());
                          return Transaction.success(mutableData);
                      }
  
                      return Transaction.abort();
                  }
  
                  @Override
                  public void onComplete(DatabaseError firebaseError, boolean commited, DataSnapshot dataSnapshot) {
                      if (commited) {
                          String email = user.getEmail();
                          String uid = user.getUid();
                          String avatar = "avatar1";
                          long mFbId = fbId;
                          String registered = Calendar.getInstance().getTime().toString();
                          String lastLogin = Calendar.getInstance().getTime().toString();
  
                          Log.w("AUTH", "username saved");
                          mDatabase.child("user").child(uid).setValue(new User(username, email, uid, avatar, mFbId, lastLogin, registered));
                      } else {
                          Log.w("AUTH", "username exists");
                      }
                  }
              });
          }
      }
  </code></pre>
  
  <p>This works, since if I try to register accounts with the same userName, nothing is created in my database. <strong>However,</strong> A user is created in my Authentication/Users. I can see this leading to all kinds of issues.</p>
  
  <p><em>How should I be forcing unique usernames in my database in a way that does not register the user until the check has been made?</em></p>
  
  <p>EDIT: To clarify, I can detect that the username is taken, and prompt the user to input another. <strong>However</strong>, what if the user gets a call/exits my app while doing this, then their email will be authenticated (i.e they cant register again), but they won't have a User-object in my database, so they will never be able to use my app. </p>