Hey Herman,
Typically when dealing with an app with user authentication, I suggest that they create a /users/ node at the root of their Firebase. This node will have children which are the uid values of the users returned from Firebase Simple Login. You should use uid because it is unique across all users on all providers, not just across one provider as is the case with id. Every time a user logs in, you would check if /users/$uid/ exists, where $uid is the logged-in user's uid. If it doesn't create a new node there and put in whatever information you want (first name, last name, email, login provider, username, path to profile picture, etc.). If the node already exists, then just retrieve all of the user's information from it. Since we can store their username in their particular node, we can easily retrieve it after they log in.
You could then have security rules which look something like this:
{
"rules": {
"users": {
"$uid": {
".read": "auth != null && auth.uid == $uid",
".write": "auth != null && auth.uid == $uid",
".validate": "newData.hasChildren('displayName', 'provider', 'provider_id')"
}
}
}
}
Since you want to ensure everyone has a unique username, you could do two things. You could go with my suggestion above and when a new user logs in, ask them for a username and loop through each child in the /users/ node (using a child_added event on the /users/ node). For each child, see if the username selected already exists. This obviously won't scale well as your app grows but would certainly work.
A better solution for you may be to use your unique username as the /users/ node's child keys instead of the uid values. Then, when a new user tries picking a username (say "jacob"), you can use a Firebase query to check if the username is taken:
usersRef.startAt(null, "jacob").endAt(null, "jacob").on("value", function(snapshot) {
if (snapshot.val() === null) {
// username not taken
} else {
// username taken
}
});
Hope that helps!
Jacob