问题
Is there a way to know if the email entered by user is real in Firebase? Does the built-in sign up with email&password method have this feature?
EDIT: sorry for the misunderstanding . I don't care if the email has been used before , what I need to know is: if the entered email is 'made-up' or 'real , exists'
回答1:
Yes, you can do that either you create new account or signing in:
For creating, read createUserWithEmailAndPassword
's Docs
createUserWithEmailAndPassword
throws 3 exceptions:
FirebaseAuthWeakPasswordException
: if the password is not strong enoughFirebaseAuthInvalidCredentialsException
: if the email address is malformedFirebaseAuthUserCollisionException
: if there already exists an account with the given email address.
You can handle that in onCompleteListener
or onFailureListener
Here an example where mAuth
is FirebaseAuth
instance:
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(
new OnCompleteListener<AuthResult>()
{
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
if (!task.isSuccessful())
{
try
{
throw task.getException();
}
// if user enters wrong email.
catch (FirebaseAuthWeakPasswordException weakPassword)
{
Log.d(TAG, "onComplete: weak_password");
// TODO: take your actions!
}
// if user enters wrong password.
catch (FirebaseAuthInvalidCredentialsException malformedEmail)
{
Log.d(TAG, "onComplete: malformed_email");
// TODO: Take your action
}
catch (FirebaseAuthUserCollisionException existEmail)
{
Log.d(TAG, "onComplete: exist_email");
// TODO: Take your action
}
catch (Exception e)
{
Log.d(TAG, "onComplete: " + e.getMessage());
}
}
}
}
);
For signing in, read signInWithEmailAndPassword
's Docs first.
signInWithEmailAndPassword
throws two exceptions:
FirebaseAuthInvalidUserException
: if email doesn't exist or disabled.FirebaseAuthInvalidCredentialsException
: if password is wrong
Here is an example:
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(
new OnCompleteListener<AuthResult>()
{
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
if (!task.isSuccessful())
{
try
{
throw task.getException();
}
// if user enters wrong email.
catch (FirebaseAuthInvalidUserException invalidEmail)
{
Log.d(TAG, "onComplete: invalid_email");
// TODO: take your actions!
}
// if user enters wrong password.
catch (FirebaseAuthInvalidCredentialsException wrongPassword)
{
Log.d(TAG, "onComplete: wrong_password");
// TODO: Take your action
}
catch (Exception e)
{
Log.d(TAG, "onComplete: " + e.getMessage());
}
}
}
}
);
回答2:
There is another solution without any create user or sign in processes.
//check email already exist or not.
firebaseAuth.fetchSignInMethodsForEmail(email)
.addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {
@Override
public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {
boolean isNewUser = task.getResult().getSignInMethods().isEmpty();
if (isNewUser) {
Log.e("TAG", "Is New User!");
} else {
Log.e("TAG", "Is Old User!");
}
}
});
回答3:
This maybe late answer but just for someone passing by could get benefit from the answer. I think the best way to check if the email is exist or not is to send the verification email to the inputing email.
Try checking out this link
https://firebase.googleblog.com/2017/02/email-verification-in-firebase-auth.html
Firebase has provided really easy way to verify user email.
回答4:
It would be almost impossible to check if the email entered exists, you can however check if the email string entered conforms to the valid email structure:
if(!isValidEmail(etEmail.getText().toString())) {
Toast.makeText(getActivity(), "Invalid input for email", Toast.LENGTH_LONG).show();
Then like Jin Liu suggests, use the sendEmailVerification(), like:
mFirebaseUser.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful())
Toast.makeText(getContext(), "Email Verfication Sent", Toast.LENGTH_LONG).show();
else {
try{
throw task.getException();
}catch (Exception e) {
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
});
来源:https://stackoverflow.com/questions/40093781/check-if-given-email-exists