【发布时间】:2019-06-05 01:00:36
【问题描述】:
我正在使用 firebase phone authentication 对 firebase 上的用户进行身份验证。它工作正常,在使用 xyz 电话号码成功验证用户后,现在我想将电话号码更新为 abc,简而言之,我正在尝试更新用户的手机号码。
我找到了 firebase web api 来更新用户现有的手机号码,其工作原理如下。
以下功能将在用户想要注册的新电话号码上发送 otp。
function sentOTPToNewNumber() {
var phoneNumber = document.getElementById("newPhone").value;
var appVerifier = window.recaptchaVerifier;
firebase.auth().signInWithPhoneNumber(phoneNumber, appVerifier)
.then(function (confirmationResult) {
// SMS sent. Prompt user to type the code from the message, then sign the
// user in with confirmationResult.confirm(code).
window.confirmationResult = confirmationResult;
}).catch(function (error) {
console.log(error)
});
}
在收到新号码的 otp 后,我们必须验证号码后面的功能是否正在执行此操作。
function codeVerification() {
var verificationCode = document.getElementById("verification").value;
try {
//at this line i am facing issue.
var credential = firebase.auth.PhoneAuthProvider.credential(confirmationResult.verificationId, verificationCode)
console.log(credential);
var result = userLocal.updatePhoneNumber(credential);
console.log(result);
} catch (error) {
console.log(error);
}
}
如果用户在 codeVerification 函数中输入错误的 otp,我想处理一个错误。当我们尝试输入错误的 otp 时,我们使用的以下 api 会引发错误,但我无法处理 try catch 块内的错误。
firebase.auth.PhoneAuthProvider.credential(confirmationResult.verificationId, verificationCode)
我在google firebase 上找到了以下描述,但无法理解如何处理错误。我实现的try catch 块没有捕获firebase 抛出的错误。我也尝试使用 then(function(){}).catch(function(error){}) 它说 then 不是函数。
在我想要处理的控制台中出现以下错误。 注意:如果用户输入正确的 otp,我能够成功更新用户电话号码。唯一的问题是我想处理代码不正确或更新是否成功的情况。
感谢您的时间和支持,这将非常有帮助。
【问题讨论】:
-
嘿,如果你解决了我也遇到了同样的问题你能检查一下这个问题吗! !/stackoverflow.com/questions/59017495/…
-
您的问题很有帮助!谢谢
标签: javascript firebase error-handling firebase-authentication