7

My app has gmail and facebook authentication integrated through Firebase. I noticed if someone signs up with their gmail then signs up with Facebook, if the Facebook had the same email as their gmail then they'll get the error:

"The email address is already in use by another account."

Is the only reasonable way to handle this to tell the user to sign in with different credentials? Maybe show a message like "Email already in use, please sign up with different account"?

MarksCode
  • 8,074
  • 15
  • 64
  • 133
  • Please take a look at this post: http://stackoverflow.com/questions/39459524/handling-linking-accounts-in-firebase – Clinton May 05 '17 at 01:45

4 Answers4

12

There are 3 ways in which you can handle this problem.

The first one is to verify if the email address exists and then 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. In other words, if a user signs up with Gmail and then signs up with Facebook and he has the same email address then 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 is 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, and 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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • hi. I have just pure sign up form, and it says that user with same email is already existing... I defo dont have that email, i type in ``asdhweruhewr@asjdaisdj.com`` and it still says its in use.... it creates the account but im not sure why there is error catch... – Rokas Devolskis Mar 30 '20 at 20:58
  • @RokasDevolskis Without seeing your code, I can not be much of a help. So please post a new question using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Mar 31 '20 at 10:42
  • I'm curious what the downsides are for having multiple accounts for a user (assuming you have them linked in some way). It actually seems super convenient. – Tony Mar 25 '21 at 19:12
  • @Tony It's not about a down-side it's more about the use-case of your app. If your app needs a user with a single email and multiple accounts then go ahead with that. – Alex Mamo Mar 25 '21 at 19:17
2

It happened to me and it was because I was calling the method wrong. use signInWithEmailAndPassword

1

Since there are a lot of similar questions to this topic I am posting my method for this error since I did not find an answer that suits a specific situation.

Lets say you have enabled an E-Mail login and also a Facebook login and a user registers via email login first and then tries to login with Facebook with the same email. Now, if you don't want to link this account with the existing one, or don't want to enable multiple accounts with one email, you can just add a Toast message that will notify the user that the email is already in use. And he cannot login via Facebook then. Here is my approach that handles the error when a user tries to register via Facebook with an already existing email:

       private void handleFacebookAccessToken(AccessToken token) {
        Log.d(TAG, "handleFacebookAccessToken:" + token);

        AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
        mAuth.signInWithCredential(credential)
                .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, "signInWithCredential:success");
                                FirebaseUser user = mAuth.getCurrentUser();
                                getTokenId();
                                updateUI(user);

                        } else {
                            Toast.makeText(SignInActivity.this, "Your Facebook email is already in use.", Toast.LENGTH_LONG).show();
                        }
                    }
                });
    }

I basically used the code provided by Firebase and added and if-else statement (if (task.isSuccessful())

Hope it will help someone!

Kaiser
  • 606
  • 8
  • 22
  • I don't think this is a good idea as the sign up could fail for other reasons (for example, weak password). You should check the error code – Tony Mar 25 '21 at 19:07
0
  void createuser(String email, String password) async {
try{
  await FirebaseAuth.instance
    .createUserWithEmailAndPassword(email: email, password: password);
}
on FirebaseAuthException catch (e){
  if(e.code == "email-already-in-use"){

   Get.showSnackbar(const GetSnackBar(
    margin: EdgeInsets.all(15),
    borderRadius: 8,
    message:
        ('There already exists an account with the given email address.'),
    duration: Duration(seconds: 3),
    backgroundColor: Colors.red,
  ));
 }
}

}

Here is an example for the 'email-already-in-use' firebase authentication error.