0

My current firebase system allows me to signup with a new user using firebase createuser() taking an email and password, In the oncomplete method I also take the details used to sign up so the email and password along with some other variables I create such as username and DOB and save them to the firebase db with the following code:

// ...database code above
DatabaseReference users = database.getReference("users"); 
User user = new User(username, password,DOB,email); 
users.push().setValue(user);
  1. I need to check now before saving to the database that the username is unique if it is not it asks for another username
  2. I have also implemented a system where when the user logs in, it checks the String if it is an email or a username, if the user logs in with an email then it'll use firebase's system to login but if they use a username then it has to check the firebase database to see if the username exists and then checks if the password is correct

I have no idea how to do any of the above, any help is much appreciated thanks. I have used the stackoverflow search and realise there are posts with this question that has already been asked, I have tried a few of these solutions that didn't work because everyones database structure is different

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
TerribleCoder
  • 27
  • 3
  • 7
  • See http://stackoverflow.com/questions/25294478/how-do-you-prevent-duplicate-user-properties-in-firebase and probably many other results in [this list](http://stackoverflow.com/search?q=%5Bfirebase-authentication%5D+unique+username) – Frank van Puffelen Jul 18 '16 at 18:55

1 Answers1

3

Let me focus on the first problem as of now. Unique Usernames.

One of the key aspects of having a greater retrieval in Firebase is structuring data. How you structure determines how efficient is your application's functionality.

For unique usernames, I have suggest something like this in your Firebase Database.

enter image description here

In the above Database Structure, I have made a separate node for usernames that not available (true because some value is required). So that every-time when I have to check, I will not have to go through other details of the USERS node.

In your Application, you can use it this way to store Username.

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference takenNames = database.getReference("TakenUserNames");

.
.
.
//On Successful Registeration
.
takenNames.child(username).setValue(true);

And while registering, you can retrieve that value like this:

public boolean doesNameExist(final String sUsername)
    {
theTakenNameRef = database.getReference("TakenUserName");
        theTakenNameRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.hasChild(sUsername))
                {
                    isTaken = true;
                }
                else if (!dataSnapshot.hasChild(sUsername))
                {
                    isTaken = false;
                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(mContext, "Connection Error. Please try again in some time.", Toast.LENGTH_SHORT).show();
            }
        });

        return isTaken;
    }
.
.
.
.
.
.
//Calling it in method
String username = editText.getText().toString().trim().toUpperCase();
boolean exists = doesNameExist(username);
if(exists)
{
//Show Error
}
else
{
//Continue Registration
}
}

Hope this clears on how efficiently unique username can be checked.

And using this mechanism, you can put your logic for the second question as well.

Tanishq Sharma
  • 538
  • 2
  • 14