1

I would like to ensure that before attempting to sign up a displayName is unique. I have a user table like this:

$uid: {
  ..data..
  displayName: 'noob839',
}

I have done an ".indexOn": ["displayName"] to optimise this, now I need to (from the client) check every user to see if displayName value is already taken.

It is very important that the current user can only read that field of another user.

Is this a recommended approach? How would I go about iterating over every user to see if the display name is unique? Thanks in advance.

P.S. Since we cannot attach data to users we must make a seperate user object which complicates app design since it adds another point of failure for sign ups, and data retrieval (a two step process). That's a shame!

Dominic
  • 62,658
  • 20
  • 139
  • 163
  • 1
    The recommended approach is to keep the values that you want to be unique in a collection as the key. So create a collection of `displayNames` and then use the (potentially encoded) display name as the key in there. See https://stackoverflow.com/questions/35243492/firebase-android-make-username-unique – Frank van Puffelen Jun 08 '17 at 12:59

1 Answers1

1

Query on that name so you are not looping through the users:

This is in Angular2 but can converted to the appropriate language (what I had already)

/*
    Available username
*/
public availUsername(displayName: string): Observable<any[]> {
    this.users = this.AF.database.list("users", {
        query: {
            orderByChild: 'displayName',
            equalTo: displayName
        }
    }).first();
    return this.users;
}

this.availUsername("theblindprophet").
   subscribe(response => {
       if(response.length == 0) {
           // No one has this displayName
       } else {
           // someone has it
       }
   });

If there are no results (array length of 0) then no one has that name.

Assumed you have users stored under users.

theblindprophet
  • 7,767
  • 5
  • 37
  • 55