简体   繁体   English

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

[英]Firebase post successful signup redirecting before sending verification email

I am chaining promises here: 我在这里链接诺言:

  1. First to send verification email 首先发送验证电子邮件
  2. Then update user profile information 然后更新用户个人资料信息
  3. Then redirect user to my app's dashboard page 然后将用户重定向到我的应用的信息中心页面

I was expecting the .then calls to happen only when the previous ones have finished execution. 我期望.then调用仅在先前的调用完成执行时发生。 But instead, redirection is happening before sending verification email or updating profile. 但相反,重定向是在发送验证电子邮件或更新配置文件之前发生的。 Below is the piece of code responsible for the same: 以下是负责相同代码的部分:

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 + '.');
}

I tried switching the 我尝试切换

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

to

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

but then none of the 3 .then() part get executed. 但是3 .then()部分都没有执行。

What am I doing wrong and how can I chain these promises to carry all 3 of the above mentioned steps? 我在做错什么,如何将这些承诺与上述所有3个步骤挂钩?

You have to return a promise from within each then() to be able to chain them. 您必须从每个then()内部返回一个Promise才能链接它们。 Lucky for you sendEmailVerification() and updateProfile() both return promises, so it's a simple change: 幸运的是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;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM