简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM