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 :