skip to Main Content

I have this function to create authentication in my firebase but the problem is when the user enters an email that already exists my app will stop com.google.firebase.auth.FirebaseAuthUserCollisionException: The email address is already in use by another account.
how I can check if the email is already exists in the authentication

fun createUserWithEmailAndPassword(email: String, password: String) {
    viewModelScope.launch(Dispatchers.IO) {
        auth
            .createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    user.userID = auth.uid!!
                    Log.d("TAG", "${auth.uid}")
                    addUser(user)
                } else {
                    Log.d("userVM" ,task.result.toString())
                }
            }
    }
}

2

Answers


  1. The FirebaseAuth#createUserWithEmailAndPassword(@NonNull email: String, @NonNull password: String) method:

    Tries to create a new user account with the given email address and password. If successful, it also signs the user into the app.

    This means that if you try to create a user that already exists, a FirebaseAuthUserCollisionException will be thrown:

    Thrown when an operation on a FirebaseUser instance couldn’t be completed due to a conflict with another existing user.

    If a user already exists, you should call another method that only signs in the user which is called
    signInWithEmailAndPassword(@NonNull email: String, @NonNull password: String):

    Tries to sign in as a user with the given email address and password.

    That doesn’t try to create the user again. If you want to check if a user already exists, you should add user details in Firestore for example, and use my solution from the following post:

    Login or Signup to reply.
  2. For those using the Pyrebase library in python. This method does not try to create a new user account with the given email address. If the email is not found.

        def does_email_exist(email, password="password"):
        try:
            auth = pyre.auth()
            auth.sign_in_with_email_and_password(email, password)
               
             # read message in HTTPError
        except requests.HTTPError as e:         
            error_json = e.args[1]
            error = json.loads(error_json)['error']['message']
            # if account with this email doesn't exist.
            if error == "USER_NOT_FOUND" or error == "EMAIL_NOT_FOUND":
                return False
        return True
    

    does_email_exist can trigger the Firebase TOO_MANY_ATTEMPTS_TRY_LATER flag, if it’s used too many times.

        existing_emails = []
        temp_email = "[email protected]"
    
        if temp_email in existing_emails or does_email_exist(temp_email):
            print("Account With Email Already Exists.")
    
            if temp_email not in existing_emails:
                 existing_emails.append(temp_email)
                
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search