How to Check If an Email Address Is Already in Use Firebase

How to check if an email address is already in use in Firebase on iOS?

You need to use fetchProvidersForEmail:completion:
https://firebase.google.com/docs/reference/ios/firebaseauth/api/reference/Classes/FIRAuth#-fetchprovidersforemailcompletion

It will return the list of provider IDs associated with that email. If you get 'password' in the list then an email/password account already exists for that email, you can show your sign-in page.

Firebase login—Check if email in use

To detect if an email is already in use, don't call createUserWithEmailAndPassword, but instead call fetchSignInMethodsForEmail .

Check if an email already exists in Firebase Auth in Flutter App

The error raised is a PlatformException
so you can do something as follows-

try {
_firbaseAuth.createUserWithEmailAndPassword(
email: 'foo@bar.com',
password: 'password'
);
} catch(signUpError) {
if(signUpError is PlatformException) {
if(signUpError.code == 'ERROR_EMAIL_ALREADY_IN_USE') {
/// `foo@bar.com` has alread been registered.
}
}
}

The following error codes are reported by Firebase Auth -

  • ERROR_WEAK_PASSWORD - If the password is not strong enough.
  • ERROR_INVALID_EMAIL - If the email address is malformed.
  • ERROR_EMAIL_ALREADY_IN_USE - If the email is already in use by a different account.

How To Check If An Email Address Is Already In Use Firebase

This answer took my quite a long time to find, but here it is.

func emailCheck(input: String, callback: (isValid: Bool) -> Void) {
FIRAuth.auth()?.signInWithEmail(input, password: " ") { (user, error) in
var canRegister = false

if error != nil {
if (error?.code == 17009) {
canRegister = false
} else if(error?.code == 17011) {
//email doesn't exist
canRegister = true
}
}

callback(isValid: canRegister)
}
}

The reason why this code works is pretty simple. Essentially if you pass a non null password into Firebase, i.e. " " instead of "" you will get some sort of error code back. The two most important return codes that I am looking for is 17009 and 17011. This is because if 17009 is returned (signifying a wrong password) then we know that there already is an email address associated with that proposed address, so therefore the user shouldn't be able to sign up for a new one. The next code, 17011 is pretty straight forward. It means that there is no user on file with that given email address, which ten allows the user to claim it. I call this function by using this:

mailCheck(emailTxt.text!) { isValid in
if isValid {
print("valid")
} else {
print("Invalid!")
}

And then manage the boolean that comes from it. Hope this helps other people in the same situation!

How to know if user's email is already registered

FirebaseAuth's createUserWithEmailAndPassword function will return an exception if there is already an account associated with that email, and for any other error that happens during signup. You can listen for this exception and act accordingly. The way I usually do this is in an async function in a seperate Auth service class:

Async Function Example:

Future<String> signUp(String email, String password) async {
AuthResult result = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
return result.user.uid;
}

Await for result:

try {
await signUp(email, password).then((uid) {
// User successfully created so Navigate to new page etc
});
} catch (e) {
print("Error in sign up: $e");
String exception = getExceptionText(e);
_showErrorAlert(
title: "Signup failed",
content: exception,
);
}

And for reference here is the getExceptionText function inside the Auth service:

String getExceptionText(Exception e) {
if (e is PlatformException) {
switch (e.message) {
case 'There is no user record corresponding to this identifier. The user may have been deleted.':
return 'User with this e-mail not found.';
break;
case 'The password is invalid or the user does not have a password.':
return 'Invalid password.';
break;
case 'A network error (such as timeout, interrupted connection or unreachable host) has occurred.':
return 'No internet connection.';
break;
case 'The email address is already in use by another account.':
return 'Email address is already taken.';
break;
default:
return 'Unknown error occured.';
}
} else {
return 'Unknown error occured.';
}
}

Firebase: The email address is already in use by another account. (auth/email-already-in-use)

Capture your error with error code :

if(errorCode == "auth/email-already-in-use"){
alert("Email already in use")
}

Java Android Check if e-mail already exists on firebase

Firebase automatically tells you if an email that you want to create an account with already exists.
When creating an account you should check if the task was succesfull and under

if(task.isSuccessful()) {} // you have this code in your last bit of code
else{} // you have this in your code already

(

you currently have the code

Toast.makeText(getApplicationContext(), "registration not performed", Toast.LENGTH_SHORT).show();

( it is pretty much the last line in the code you provided) but you should replace it with:

try {
throw task.getException();
} catch(FirebaseAuthUserCollisionException e) {
// email already in use
Toast.makeText(getApplicationContext(), "Email already taken!", Toast.LENGTH_SHORT).show();
}

So you do not need to check if an email exists yourself because Firebase will automatically throw an exception and then you can for example display a toast.

How to find out if an email is already registered with Firebase Simple Login?

Update 7/19/2018

The previous mention method is deprecated. Now fetchSignInMethodsForEmail is used.

Update 6/26/2016

A lot has changed since this was originally posted, Firebase Authentication is no longer called Firebase Simple Login.

Additionally, it looks like a new method is available fetchProvidersForEmail which might provide a solution to this problem.

From the docs:

fetchProvidersForEmail(email) returns firebase.Promise containing non-null Array of string

Gets the list of provider IDs that can be used to sign in for the given email address. Useful for an "identifier-first" sign-in flow.

I have not tested this but I'd imagine if you call that method and receive an array of providers then the e-mail must already be registered with one or more authentication providers.

Old Answer

I don't believe you can query the Firebase Simple Login directly for that information without either trying to actually login the user or register them.

What you'd need to do is store a list of registered e-mails during your registration process and query that list instead. Then you can query that path and if it comes back with data, it exists, otherwise it doesn't.

Firebase Auth and Swift: Check if email already in database

If you are using email & password authentication, the solution is very simple.

Firebase Authentication will not allow duplicate emails so when the createUser function is executed, if the email already exists Firebase will return a emailAlreadyInUse error in the error parameter. You can then cast this to an NSError to see which one it is and handle appropriately.

So the function is like this

Auth.auth().createUser(withEmail: createEmail, password: password ) { user, error in
if let x = error {
let err = x as NSError
switch err.code {
case AuthErrorCode.wrongPassword.rawValue:
print("wrong password")
case AuthErrorCode.invalidEmail.rawValue:
print("invalid email")
case AuthErrorCode.accountExistsWithDifferentCredential.rawValue:
print("accountExistsWithDifferentCredential")
case AuthErrorCode.emailAlreadyInUse.rawValue: //<- Your Error
print("email is alreay in use")
default:
print("unknown error: \(err.localizedDescription)")
}
//return
} else {
//continue to app
}

I threw some random errors into that case statement but check the link for a complete list of all AuthErrorCodes.

You can also do this

Auth.auth().fetchSignInMethods(forEmail: user, completion: { (signInMethods, error) in
print(signInMethods)
})


Related Topics



Leave a reply



Submit