createUserWithEmailAndPassword 重定向导致错误

createUserWithEmailAndPassword redirect causes an error

我想在注册后将用户重定向到我网站的一部分,而不是登录 因此,在 createUserWithEmailAndPassword 函数的末尾,我更改了 window.location 并且一切正常,但由于函数被中断

,我确实收到了一个错误

如何避免这种情况?有 signInWithRedirect 和 getRedirectResult 但 Firebase 文档不包括电子邮件作为提供者,只有 facebook

我可以尝试捕获错误,但也许有更优雅的方法来解决这个问题?

function handleSignUp() {
    var email = document.getElementById('email').value;
    var password = document.getElementById('password').value;
    if (email.length < 4) {
        alert('Please enter an email address.');
        return;
    }
    if (password.length < 4) {
        alert('Please enter a password.');
        return;
    }
    firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
        var errorCode = error.code;
        var errorMessage = error.message;
        if (errorCode == 'auth/weak-password') {
            alert('The password is too weak.');
        } else {
            alert(errorMessage);
        }
        console.log(error);
    });
    window.location = "/add-location";
}

// 控制台错误

A network error (such as timeout, interrupted connection or unreachable host) has occured.

登录回调后重定向呢?

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in. So redirect:
     window.location = "/add-location";
  } else {
    // No user is signed in.
  }
});

docs中所述,创建后,身份验证状态会自动更改为已登录,以便在成功登录后触发重定向。

注册成功后直接重定向:

firebase.auth().createUserWithEmailAndPassword(email, password)
  .then(function(user) {
    window.location = "/add-location";
  })
  .catch(function(error) {
    ...
  });

您在 promise 解决之前进行了重定向,因此出现中断操作错误。