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

时间:2017-07-28 21:30:13

标签: javascript firebase promise firebase-authentication

我在这里链接承诺:

  1. 首先发送验证邮件
  2. 然后更新用户个人资料信息
  3. 然后将用户重定向到我应用的信息中心页面
  4. 我期待.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(()=&gt; function(){...})

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

    我做错了什么?我如何将这些承诺链接到上述所有3个步骤?

1 个答案:

答案 0 :(得分:1)

您必须在每个then()内返回一个承诺,以便能够将它们链接起来。幸运的是你sendEmailVerification()updateProfile()都返回了承诺,所以这是一个简单的改变:

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