我需要帮助。我不知道如何检测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');
}
}
发布于 2020-04-30 01:31:15
在检查和重定向之前,您必须等待异步函数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();
});
}
https://stackoverflow.com/questions/61494806
复制相似问题