我是否错误地调用了onAuthStateChanged函数?

时间:2017-09-13 05:39:30

标签: javascript firebase firebase-authentication

githubLogin.addEventListener('click', () => {
    var provider = new firebase.auth.GithubAuthProvider();

    firebase.auth().signInWithPopup(provider).then(function(result) {
        var user = result.user;
        console.log(user);
        firebase.auth().onAuthStateChanged(function(user) {
            if (user) {
                window.location = "pages/welcome.html";
            }});
    }).catch(function(error) {
        console.log(error);
    });

});

所以问题在于,当我将用户重定向到欢迎时,我发现该页面上没有用户登录。 我知道我必须使用onAuthStateChanged()函数,但到目前为止我已经错了。我能知道怎么做吗?而且我也有类似的谷歌登录,是否会影响功能?

1 个答案:

答案 0 :(得分:0)

在显示按钮之前,您应该确定Auth状态。如果onAuthStateChanged侦听器最初使用空用户触发,则显示登录屏幕。

同样,当您致电signInWithPopup时,您不需要添加状态更改侦听器。你可以这样做:

firebase.auth().signInWithPopup(provider).then(function(result) {
  // User logged in, you can get currentUser from onAuthStateChanged
  // listener on welcome page.
  window.location.assign("pages/welcome.html");
}).catch(function(error) {
  // Handle error.
});