title: Enforcing unique usernames with Firebase simplelogin
body: <p>I have recently followed a tutorial over on <a href="https://thinkster.io/learn-to-build-realtime-webapps/" rel="nofollow noreferrer">Thinkster</a> for creating a web app using Angular and Firebase.</p>
  
  <p>The tutorial uses the Firebase simpleLogin method allows a 'profile' to be created that includes a username.</p>
  
  <p><strong>Factory:</strong></p>
  
  <pre><code>app.factory('Auth', function($firebaseSimpleLogin, $firebase, FIREBASE_URL, $rootScope) {
  var ref = new Firebase(FIREBASE_URL);
  
  
  var auth = $firebaseSimpleLogin(ref);
  
  var Auth = {
      register: function(user) {
         return auth.$createUser(user.email, user.password);
      },
      createProfile: function(user) {
          var profile = {
              username: user.username,
              md5_hash: user.md5_hash
          };
  
          var profileRef = $firebase(ref.child('profile'));
          return profileRef.$set(user.uid, profile);
      },
      login: function(user) {
          return auth.$login('password', user);
      },
      logout: function() {
          auth.$logout();
      },
      resolveUser: function() {
          return auth.$getCurrentUser();
      },
      signedIn: function() {
          return !!Auth.user.provider;
      },
      user: {}
  };
  
  $rootScope.$on('$firebaseSimpleLogin:login', function(e, user) {
      angular.copy(user, Auth.user);
      Auth.user.profile = $firebase(ref.child('profile').child(Auth.user.uid)).$asObject();
  
      console.log(Auth.user);
  });
  $rootScope.$on('$firebaseSimpleLogin:logout', function() {
      console.log('logged out');
  
      if (Auth.user &amp;&amp; Auth.user.profile) {
          Auth.user.profile.$destroy();
      }
      angular.copy({}, Auth.user);
  });
  
  return Auth;
  });
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>$scope.register = function() {
      Auth.register($scope.user).then(function(user) {
          return Auth.login($scope.user).then(function() {
              user.username = $scope.user.username;
              return Auth.createProfile(user);
          }).then(function() {
              $location.path('/');
          });
      }, function(error) {
          $scope.error = error.toString();
      });
  };
  </code></pre>
  
  <p>At the very end of the tutorial there is a 'next steps' section which includes:</p>
  
  <blockquote>
    <p>Enforce username uniqueness-- this one is tricky, check out Firebase priorities and see if you can use them to query user profiles by username</p>
  </blockquote>
  
  <p>I have searched and searched but can't find a clear explanation of how to do this, particularly in terms of the <a href="https://www.firebase.com/docs/web/api/firebase/setpriority.html" rel="nofollow noreferrer">setPriority()</a> function of Firebase</p>
  
  <p>I'm quite the Firebase newbie so any help here would be gratefully recieved.</p>
  
  <p>There are a few <a href="https://stackoverflow.com/questions/25294478/how-do-you-prevent-duplicate-user-properties-in-firebase">similar questions</a>, but I can't seem to get my head around how to sort this out.</p>
  
  <p>Enormous thanks in advance.</p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>From Marein's answer I have updated the register function in my controller to:</p>
  
  <pre><code>$scope.register = function() {
  
      var ref = new Firebase(FIREBASE_URL);
      var q = ref.child('profile').orderByChild('username').equalTo($scope.user.username);
      q.once('value', function(snapshot) {
          if (snapshot.val() === null) {
              Auth.register($scope.user).then(function(user) {
                  return Auth.login($scope.user).then(function() {
                      user.username = $scope.user.username;
                      return Auth.createProfile(user);
                  }).then(function() {
                      $location.path('/');
                  });
              }, function(error) {
                  $scope.error = error.toString();
              });
          } else {
              // username already exists, ask user for a different name
          }
      });
  };
  </code></pre>
  
  <p>But it is throwing an 'undefined is not a function' error in the line <code>var q = ref.child('profile').orderByChild('username').equalTo($scope.user.username);</code>. I have commented out the code after and tried just console.log(q) but still no joy.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>The issue with the above was that the Thinkster tutorial uses Firebase 0.8 and orderByChild is available only in later versions. Updated and Marein's answer is perfect.</p>