Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with Email address already in use - Firebase Authentication

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"?

like image 281
MarksCode Avatar asked May 05 '17 01:05

MarksCode


3 Answers

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

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.

like image 75
Alex Mamo Avatar answered Oct 13 '22 00:10

Alex Mamo


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

like image 37
Ruben Palavecino Avatar answered Oct 12 '22 23:10

Ruben Palavecino


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!

like image 40
Kaiser Avatar answered Oct 12 '22 23:10

Kaiser