Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Link Firebase Phone Authentication with Email/Password Authentication?

I am trying to create an app in which after a user types his email/ password they are saved in the firebase then the user enters his phone number on which otp is sent and the user is logged in after verification.my problem is that when both of these steps are completed firebase is creating two separate accounts one with email other with the phone. Please Tell me how can I create a Single account with Both Email/Password and Phone.

like image 683
DarkShadow Avatar asked Mar 30 '18 08:03

DarkShadow


People also ask

How do I use Firebase email and password authentication?

If you haven't yet connected your app to your Firebase project, do so from the Firebase console. Enable Email/Password sign-in: In the Firebase console, open the Auth section. On the Sign in method tab, enable the Email/password sign-in method and click Save.

How can I get my Firebase authentication password?

Finding the Password Hash Parameters To access these parameters, navigate to the 'Users' tab of the 'Authentication' section in the Firebase Console and select 'Password Hash Parameters' from the drop down in the upper-right hand corner of the users table.


1 Answers

Since you are using multiple Firebase authentication providers then you need to link them, so both phone and email will create on single account.

First you can get the credentials:

AuthCredential credential = EmailAuthProvider.getCredential(email, password);

then using linkwithCredentials() you will be able to link them:

mAuth.getCurrentUser().linkWithCredential(credential)
    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                Log.d(TAG, "linkWithCredential:success");
                FirebaseUser user = task.getResult().getUser();
                updateUI(user);
            } else {
                Log.w(TAG, "linkWithCredential:failure", task.getException());
                Toast.makeText(AnonymousAuthActivity.this, "Authentication failed.",
                        Toast.LENGTH_SHORT).show();
                updateUI(null);
            }

            // ...
        }
    });

more info here:

https://firebase.google.com/docs/auth/android/account-linking

like image 111
Peter Haddad Avatar answered Oct 23 '22 15:10

Peter Haddad