gpt4 book ai didi

javascript - Firebase 在发送验证电子邮件之前发布成功注册重定向

转载 作者:行者123 更新时间:2023-12-03 03:46:09 25 4
gpt4 key购买 nike

我在这里链接 promise :

  1. 首先发送验证电子邮件
  2. 然后更新用户个人资料信息
  3. 然后将用户重定向到我应用的仪表板页面

我期望 .then 调用仅在前面的调用完成执行后才会发生。但相反,重定向是在发送验证电子邮件或更新个人资料之前发生的。下面是负责相同功能的代码段:

function authorizeWithFireBase(email,displayName,password,photoURL){
var user = null;
var failText = document.getElementById("signup-invalid-message");

//NULLIFY EMPTY ARGUMENTS
for (var i = 0; i < arguments.length; i++) {
arguments[i] = arguments[i] ? arguments[i] : null;
}
auth.createUserWithEmailAndPassword(email, password)
.then(function () {
user = auth.currentUser;
user.sendEmailVerification();
})
.then(function () {
user.updateProfile({
displayName: displayName,
photoURL: photoURL
});
})
.then(function () {
window.location.replace("/path/to/private/app/dashboard/page");
})
.catch(function(error) {
if(error.code == 'auth/weak-password') {
failText.innerHTML = "Please set a complex password!"
}
else {
failText.innerHTML = error.message;
}
});
console.log('Validation link was sent to ' + email + '.');
}

我尝试切换

.then(function() {...})

.then( () => function() {...})

但是 3 .then() 部分都没有被执行。

我做错了什么以及如何链接这些 promise 来执行上述所有 3 个步骤?

最佳答案

您必须从每个 then() 中返回一个 promise 才能链接它们。幸运的是你sendEmailVerification()updateProfile()两个都返回 promise ,所以这是一个简单的更改:

auth.createUserWithEmailAndPassword(email, password)
.then(function () {
user = auth.currentUser;
return user.sendEmailVerification();
})
.then(function () {
return user.updateProfile({
displayName: displayName,
photoURL: photoURL
});
})
.then(function () {
window.location.replace("/path/to/private/app/dashboard/page");
})
.catch(function(error) {
if(error.code == 'auth/weak-password') {
failText.innerHTML = "Please set a complex password!"
}
else {
failText.innerHTML = error.message;
}
});

关于javascript - Firebase 在发送验证电子邮件之前发布成功注册重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45382691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com