Firebase: reauthenticate with phoneNumber

人盡茶涼 提交于 2020-05-16 08:53:07

问题


I know that I can reauthenticate with email like this.

var user = firebase.auth().currentUser;
var credentials = firebase.auth.EmailAuthProvider.credential(
  user.email,
  'yourpassword'
);
user.reauthenticateWithCredential(credentials);

But how can I reauthenticate if I use phoneNumber as my sign in method?


回答1:


It is very similar to how you sign in with phone number. You can do it in 2 ways:

Using reauthenticateWithPhoneNumber:

const recaptchaVerifier = new firebase.auth.RecaptchaVerifier(
    container, parameters, firebase.app());
recaptchaVerifier.render();
user.reauthenticateWithPhoneNumber(user.phoneNumber, recaptchaVerifier)
  .then((confirmationResult) => {
    return confirmationResult.confirm(prompt('Enter your SMS code'));
  })
  .then((userCredential) => {
    // User successfully reauthenticated.
  });

Using reauthenticateWithCredential:

const recaptchaVerifier = new firebase.auth.RecaptchaVerifier(
    container, parameters, firebase.app());
recaptchaVerifier.render();
const phoneAuthProvider = new firebase.auth.PhoneAuthProvider(auth);
phoneAuthProvider.verifyPhoneNumber(user.phoneNumber, recaptchaVerifier)
  .then((verificationId) => {
    const credential = firebase.auth.PhoneAuthProvider.credential(
        verificationId, prompt('Enter your code'));
    return user.reauthenticateWithCredential(credential);
  })
  .then((userCredential) => {
    // User successfully reauthenticated.
  });


来源:https://stackoverflow.com/questions/54649277/firebase-reauthenticate-with-phonenumber

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!