Friday, 25 December 2020

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



from Firebase how to link account created with phoneNumber

No comments:

Post a Comment