Firebase how to link account created with phoneNumber

✅ I am able to let user update their profile with mobile number using verifyPhoneNumber and update currentUser.updatePhoneNumber

❌ My problem comes when I allow login by phone, and a NEW USER tries signing in with a phone number, an account is automatically created.

If I then need to associate the number with an email account, the above method will return credential-already-in-use error.

Firebase recommends the following method in their documentation.

var credential = firebase.auth.PhoneAuthProvider.credential(confirmationResult.verificationId, code);

firebase.auth().signInWithCredential(credential);

So I did this together linkWithCredential however, signInWithCredential returns a result with result.credential = null

// Sign in user with the account you want to link to
auth.signInWithCredential(credential).then(function(result) {
  console.log("Sign In Success", result);
  var currentUser = result.user;

  return prevUser.linkWithCredential(result.credential) //this line doesn't work.
    .then(function(linkResult) {

      return auth.signInWithCredential(linkResult.credential);
    })
}).catch(function(error) {

  console.log("Sign In Error", error);

});

Since result.credential = null, I am unable to proceed with LinkWithCredential.

I’ve also tried linkWithPhoneNumber, but it returns a validation ID, which I still cannot merge the account.

❓May I know how did you guys merge a phoneNumber account with other account(Facebook/google/email)?

Answer

If you want to link an existing account with a phone number you can use linkWithPhoneNumber. Basically you after user sign in (let say with Google), you retrieve user info and then later on link that credential with the phone number. I found a detail answer that is similar to your question link here