How to check "user email already exists" in firebase using Android Studio
this method works to check either email existed or not
void checkEmailExistsOrNot(){
firebaseauth.fetchSignInMethodsForEmail(email.getText().toString()).addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {
@Override
public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {
Log.d(TAG,""+task.getResult().getSignInMethods().size());
if (task.getResult().getSignInMethods().size() == 0){
// email not existed
}else {
// email existed
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
}
fetchProvidersForEmail is an async call so you have to make use of its return value with the callback.
return true on the main thread will not work.
Here is the solution :
First Create an interface with the method (success)
public interface OnEmailCheckListener(){
void onSuccess(boolean isRegistered);
}
Your checkEmail Method should be like this:
public void isCheckEmail(final String email,final OnEmailCheckListener listener){
mAuth.fetchProvidersForEmail(email).addOnCompleteListener(new OnCompleteListener<ProviderQueryResult>()
{
@Override
public void onComplete(@NonNull Task<ProviderQueryResult> task)
{
boolean check = !task.getResult().getProviders().isEmpty();
listener.onSuccess(check);
}
});
}
Finally call your isCheckEmail like this :
isCheckEmail("[email protected]",new OnEmailCheckListener(){
@Override
void onSuccess(boolean isRegistered){
if(isRegistered){
//The email was registered before
} else {
//The email not registered before
}
}
});
Hope this helps you.