TerribleCoder TerribleCoder - 4 months ago 69
Android Question

firebase duplicate username and custom sign-in

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

Answer

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.

Comments