Check If An Email Exists Before Creating A User Firebase Android
Solution 1:
Have an index on the email field. Then you can do a query to find by email.
That will improve performance as no need to loop through the children.
Something like:
orderByChild('email').equalTo(email).once('value').exist();
Solution 2:
Data is loaded from Firebase asynchronously, which makes it impossible to have a method like boolean validateEmail(String email)
on Android. Loading data from the database would have to be a blocking operation to enable such a method, and Android doesn't allow blocking operations (as it would leave the phone inoperable).
So instead, you have to *either( move the code to create the user intovalidateEmail
or pass in a custom interface that you then call once the result from the database is back. The code for that last one is below.
First we'll create a custom interface that you can implement anywhere where you need to check if a user already exists.
publicinterfaceUserExistsCallback {
voidonCallback(boolean value);
}
This interface can be as specific or generic as you want. It's very similar to Firebase's ValueEventListener
, but this interface is just for your own use.
With this interface, we can define the validateEmail
method like this:
privatevoidvalidateEmail(String email, final UserExistsCallback callback) {
isValid = true;
databaseUser.orderByChild("email").equalTo(emailUserEntered)
.addListenerForSingleValueEvent(newValueEventListener() {
@OverridepublicvoidonDataChange(@NonNull DataSnapshot dataSnapshot) {
callback.onCallback(dataSnapshot.exists())
}
@OverridepublicvoidonCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
And then invoke it from onCreateAccount
like this:
publicvoidonCreateAccount(View view){
Stringemail= etEmail.getText().toString().trim();
validateEmail(email), newUserExistsCallback() {
publicvoidonCallback(boolean exists) {
if (!exists) {
Stringid= databaseUser.push().getKey();
Useruser=newUser(id, email);
databaseUser.child(user.getId()).setValue(user);
Intentintent=newIntent(getApplicationContext(), LoginActivity.class);
intent.putExtra("isCreateAccount", true);
startActivityForResult (intent,0);
}
})
}
}
Also see many of the (linked) answers below:
Solution 3:
Stopping the user from logging in if such email exists should be simple and you may use a code like this to do so:
DatabaseReferencedatabaseReference= FirebaseDatabase.getInstance().getReference().child("User");
boolean flag=false;
databaseReference.orderByChild("email").equalTo(emailUserEntered).addListenerForSingleValueEvent(newValueEventListener() {
@OverridepublicvoidonDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
flag=true;
}
@OverridepublicvoidonCancelled(@NonNull DatabaseError databaseError) {
}
});
return flag;
But if you want the user to be stopped from writing an email which is in database in mid-way, without authentication, it would be pretty difficult.
You'd have to run a check through the database letter by letter and not only this would decrease efficiency, it would be not so good looking at the end.
EDIT:
Data is loaded asynchronously from Firebase so you should be placing something like this right inside the if(dataSnapshot.exists())
to avoid the issue you're facing right now.
if(validateEmail(email)){
Stringid= databaseUser.push().getKey();
Useruser=newUser(id, email);
databaseUser.child(user.getId()).setValue(user);
Intentintent=newIntent(getApplicationContext(), LoginActivity.class);
intent.putExtra("isCreateAccount", true);
startActivityForResult (intent,0);
}
else
Toast(YourActivity.this, "Email already exists", LENGTH_LONG).show();
Solution 4:
This method is for auth only: firebase.auth().fetchProvidersForEmail("emailaddress@gmail.com")
Post a Comment for "Check If An Email Exists Before Creating A User Firebase Android"