Reputation:
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'
Upvotes: 28
Views: 42727
Reputation: 607
EDIT: To check if user is 'made-up' or 'real', just send an email verification by calling FirebaseAuth.instance.currentUser.sendEmailVerification()
and ask user to click the link sent in their email, if user is able to perform this action then email exists otherwise it's 'made-up'. Later you can check if email is verified or not by using this boolean FirebaseAuth.instance.currentUser.emailVerified
. Hope this answers your question.
OLD ANSWER: In case someone is looking for a flutter answer, then a rather easiest way would be to just check if List<String>
returned by fetchSignInMethodsForEmail
contains 'password'
string or not.
Here is sample code:
var methods = await FirebaseAuth.instance.fetchSignInMethodsForEmail(email);
if (methods.contains('password')) {
/// Email exists
}
else {
/// Email does not exist
}
Original source (google codelabs): https://github.com/flutter/codelabs/blob/4397b60355daab38e98ca967c47af9b7671e0fd9/firebase-get-to-know-flutter/step_05/lib/main.dart#L122
Upvotes: 4
Reputation: 717
This 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!");
}
}
});
Upvotes: 40
Reputation: 33
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();
}
}
}
});
Upvotes: -3
Reputation: 3737
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.
Upvotes: 1
Reputation: 1526
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 malformed
FirebaseAuthUserCollisionException
: 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 wrongHere 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());
}
}
}
}
);
Upvotes: 31