javascript - 如何在 firebase 中链接电话号码后重定向?

标签 javascript firebase firebase-authentication

我需要帮助。我不知道如何检测电话号码是否已成功链接到用户。如果操作成功,我想重定向到其他页面。我的代码是这样的。 *我的代码可以工作,但如果我添加一个函数来重定向它就不起作用。

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');
 }
 }

最佳答案

在检查和重定向之前,您必须等待异步函数 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();
  });
}

关于javascript - 如何在 firebase 中链接电话号码后重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61494806/

相关文章:

ios - 处理从用于 Node 身份验证的 iOS 生成的 Firebase 中的一小时 token 过期

firebase - 在 Firebase 用户表上添加额外详细信息

firebase-authentication - 方法 'signInWithCredential' 未定义

javascript - 如何使用 JavaScript 转换 XML 和 XSLT?

javascript - 获取正整数的负数求和结果

Angular 2 Firebase Hosting 似乎需要哈希标记

android - Firestore 安全规则 : 'request.auth.uid' Is Null With Android Request

php - 将多个字段合并为一个值以进行表单提交

javascript - 将值从 HTML 控件复制到 ASP.NET 用户控件

javascript - Firebase Cloud Functions https.onCall 已完成,状态代码为 : 204