DarkShadow
DarkShadow

Reputation: 197

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.

Upvotes: 9

Views: 3497

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

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

Upvotes: 4

Related Questions