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
The FirebaseAuth#createUserWithEmailAndPassword(@NonNull email: String, @NonNull password: String) method:
This means that if you try to create a user that already exists, a FirebaseAuthUserCollisionException will be thrown:
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):
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:
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.
does_email_exist can trigger the Firebase TOO_MANY_ATTEMPTS_TRY_LATER flag, if it’s used too many times.