0

I has some problem when I try to create new account[email, password]in my app using FirebaseAuth. I want to detect if email is already use in other account. For example, I want to create account a@b.c in my app, but I'm already using this email to login by facebook. So, Is it possible to detect FirebaseAuthUserCollisionException in Firebase.

This is my code.

mAuth.createUserWithEmailAndPassword(edt1.getText().toString(), edt2.getText().toString())
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "createUserWithEmail:success");
                        FirebaseUser user = mAuth.getCurrentUser();
                        startActivity( new Intent( NewRegisterForEmali.this, NewLoginActivity.class));
                        finish();
                    }else if(task.getException().equals("com.google.firebase.auth.FirebaseAuthUserCollisionException")){
                        Log.d(TAG, "Collision!");
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "createUserWithEmail:failure", task.getException());
                        Toast.makeText(NewRegisterForEmali.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                        task.getException();
                    }

                    // ...
                }
            });

Logcat:

com.google.firebase.auth.FirebaseAuthUserCollisionException: The email address is already in use by another account.
                                                                                        at com.google.android.gms.internal.zzeaw.zzaw(Unknown Source)
                                                                                        at com.google.android.gms.internal.zzdzu.zza(Unknown Source)
                                                                                        at com.google.android.gms.internal.zzebh.zzax(Unknown Source)
                                                                                        at com.google.android.gms.internal.zzebk.onFailure(Unknown Source)
                                                                                        at com.google.android.gms.internal.zzeay.onTransact(Unknown Source)
                                                                                        at android.os.Binder.execTransact(Binder.java:565)
Hello World
  • 740
  • 5
  • 18

2 Answers2

1

maybe this link will help

Dealing with Email address already in use - Firebase Authentication

answer by @alex mamo

The first one is to verify if the email address exists and than display a message. This is exactly what you said. The message is up to you.

The second approach is to enable users to have multiple accounts per email address. With other words, if a user signs up with gmail and then signs up with Facebook and he has the same email address, than he ends up having 2 different accounts. A single email address, 2 different accounts This is not a good practice but according to your needs, you can even use it.

The third approach is to have only one account per email address. This means that you are preventing the users from creating multiple accounts using the same email address with different authentication providers. This a common practice and also the default rule in the Firebase console. This means, that you'll want to implement later another kind of authentication with another provider, it will follow the same rule. In this case, will have a single email address with a single account.

To enable or disable this option, go to your Firebase console, choose Authentication, select the SIGN-IN METHOD tab and at the bottom of your page you'll find the Advanced section.

Hope it helps.

BiRjU
  • 733
  • 6
  • 23
  • Sorry so much. That link help me but still not the answer I accepted so i vote this as useful instead of answer. I want each email that can authenticate by every provider. So one email is one account but can use every provider to sign in. – Hello World Apr 14 '18 at 10:24
0

If you are using Kotlin make sure to use,

if (task.exception is FirebaseAuthUserCollisionException) 
{
    // Handle user collision exception here
    // ...
} 
else 
{
    // Handle other exceptions here
    // ...
}
Sumit Pathak
  • 529
  • 2
  • 7
  • 24