0

How do I check for existing usernames in Firebase Realtime Database? Like.. I wish to inform the user while filling the EditText 'username' below, that his/her chosen username is already taken by someone else. So they need to choose some other username. How do I do that? Please Help.. I am stuck..

private static final String TAG = "CompleteTheRegistratio";
EditText firstname, lastname, mobno, username, useremail, useraddress;
Button submitUserDetailsButton;
FirebaseAuth firebaseAuth;
String fname, lname, mno, uname, uemail, uadd, fcm;
TextView userFcm;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_complete_the_registration);

    ActionBar actionBar = getSupportActionBar();
    assert actionBar != null;
    actionBar.hide();

    firstname=findViewById(R.id.etFirstName);
    lastname=findViewById(R.id.etLastName);
    mobno=findViewById(R.id.etCellphone);
    username=findViewById(R.id.etUserName);
    useremail=findViewById(R.id.etEmail);
    useraddress=findViewById(R.id.etAddress);
    userFcm=findViewById(R.id.tvUserFcm);
    submitUserDetailsButton=findViewById(R.id.btnSubmitUserDetails);
    firebaseAuth = FirebaseAuth.getInstance();

    submitUserDetailsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(firstname.getText().toString().isEmpty() || lastname.getText().toString().isEmpty() || mobno.getText().toString().length()<10 || username.getText().toString().isEmpty()){
                Toast.makeText(CompleteTheRegistrationActivity.this, "Please enter all required fields", Toast.LENGTH_SHORT).show();
            } else {
                    sendUserData();
                    Toast.makeText(CompleteTheRegistrationActivity.this, "Registration Accepted!", Toast.LENGTH_LONG).show();
                    startActivity(new Intent(CompleteTheRegistrationActivity.this, DashboardActivity.class));
                    finish();
            }
        }
    });

    // Generating FCM Token and storing it in a TextView :
    FirebaseMessaging.getInstance().getToken()
            .addOnCompleteListener(new OnCompleteListener<String>() {
                @Override
                public void onComplete(@NonNull Task<String> task) {
                    if (!task.isSuccessful()) {
                        Log.w(TAG, "Fetching FCM registration token failed", task.getException());
                        return;
                    }

                    // Get new FCM registration token
                    String token = task.getResult();

                    // Log and toast
                    String msg = getString(R.string.msg_token_fmt, token);
                    Log.d(TAG, msg);
                    Toast.makeText(CompleteTheRegistrationActivity.this, "Token Generated!", Toast.LENGTH_SHORT).show();
                    userFcm.setText(msg);
                }
            });
}
private void sendUserData() {
    // now finally, sending user data to Realtime Database with myRef Reference with fcm token.
    fname = firstname.getText().toString();
    lname = lastname.getText().toString();
    mno = mobno.getText().toString();
    uname = username.getText().toString();
    uemail = useremail.getText().toString();
    uadd = useraddress.getText().toString();
    fcm = userFcm.getText().toString();
    FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    DatabaseReference myRef = firebaseDatabase.getReference(firebaseAuth.getUid());
    UserProfile userProfile = new UserProfile(fname, lname, mno, uname, uemail, uadd, fcm);
    myRef.setValue(userProfile);
    // for sending user's usernames to Realtime Database inside their Uids : 
    FirebaseDatabase firebaseDatabase1 = FirebaseDatabase.getInstance();
    DatabaseReference myRef1 = firebaseDatabase1.getReference("users");
    //DisplayNameModel displayNameModel = new DisplayNameModel(firebaseAuth.getUid());
    myRef1.child(firebaseAuth.getUid()).setValue(uname);
}

Below is my firebase structure :

enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Does this answer your question? [Firebase android : make username unique](https://stackoverflow.com/questions/35243492/firebase-android-make-username-unique) or [this](https://stackoverflow.com/questions/67819240) – Dharmaraj Jul 01 '21 at 09:24
  • To ensure that anything is unique, you need to use that value as the key. in the database. So you'll want to add a `usernames` node, and store the user names there. This has been covered quite a few times before, so I linked some of those to get you started. – Frank van Puffelen Jul 01 '21 at 14:20
  • the second one helped me out. Thank you so much.. @Dharmaraj – Vaibhav Gupta Jul 02 '21 at 06:02
  • I will try that out for sure today :) @FrankvanPuffelen – Vaibhav Gupta Jul 02 '21 at 06:03

0 Answers0