Как перенаправить после ссылки phoneNumber в firebase? - PullRequest
0 голосов
/ 29 апреля 2020

Мне нужна помощь. Я не знаю, как определить, был ли phoneNumber успешно связан с пользователем. Я хочу перенаправить на другую страницу, если действие прошло успешно. Мой код такой. * Мой код работает, но если я добавляю функцию для перенаправления, она не работает.

function verifyNumber(){

var user = firebase.auth().currentUser;
var phoneNumber = document.getElementById('phoneNumber');
var signInButtonElement = document.getElementById('sign-in-button');
var inputCode = document.getElementById('code');
var codeButton = document.getElementById('confirm-code');


if(!user.phoneNumber){

  // You also need to provide a button element signInButtonElement
  // which the user would click to complete sign-in.
  // Get recaptcha token. Let's use invisible recaptcha and hook to the button.
  var appVerifier = new firebase.auth.RecaptchaVerifier(
      signInButtonElement, {size: 'invisible'});
  // This will wait for the button to be clicked the reCAPTCHA resolved.
  user.linkWithPhoneNumber(phoneNumber.value, appVerifier)
    .then(function(confirmationResult) {
      // Ask user to provide the SMS code.
      phoneNumber.style.display = 'none';
      signInButtonElement.style.display = 'none';

      //inputCode.style.display = 'inline-block'
      //codeButton.style.display = 'inline-block'
       var code = window.prompt('Provide your SMS code');
      // Complete sign-in.
      return confirmationResult.confirm(code);
      //updateStatus() doesnt work
    });
    }

 }

 function updateStatus(){
 var user = firebase.auth().currentUser;
 if(user.phoneNumber){
    window.location.href = "../home/home.html";
 }else{
    alert('Ha ocurrido un error');
 }
 }

1 Ответ

1 голос
/ 30 апреля 2020

Необходимо дождаться завершения асин c функции confirmationResult.confirm, прежде чем проверять и перенаправлять.

user.linkWithPhoneNumber(phoneNumber.value, appVerifier)
  .then(function(confirmationResult) {
    // Ask user to provide the SMS code.
    phoneNumber.style.display = 'none';
    signInButtonElement.style.display = 'none';

    //inputCode.style.display = 'inline-block'
    //codeButton.style.display = 'inline-block'
    var code = window.prompt('Provide your SMS code');
    // Complete sign-in.
    return confirmationResult.confirm(code);
  })
  .then(() => {
    // Wait for linking to complete before redirecting.
    updateStatus();
  });
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...