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

标签 javascript firebase promise firebase-authentication

我在这里链接 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/

相关文章:

android - 一段时间后 Firebase onDataChange 未调用打开应用程序

android - 获取 FirebaseInstanceId : background sync failed: TIMEOUT error while trying to get an instance ID

javascript - 如何修复我的 promise return here for this function?

javascript - 为什么它显示为未定义/NaN?

javascript - Backbonejs 应用程序在导航到 `?state=#users` 时自动加载页面 `#users`

cordova - Firebase native Cordova 插件

javascript - React-Native:解析 JSON

javascript - promise 中的 promise

javascript - 单击子组件按钮时卸载组件//React

javascript - 操作 jquery 对话框的 div 内容