2017-09-13 28 views
0
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()函數,但到目前爲止我錯了。我可以知道如何做到這一點嗎?而且我也有類似的谷歌登錄,是影響功能?我是否錯誤地調用了onAuthStateChanged函數?

回答

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

我做了這樣的事情.....但它似乎沒有再工作.. https://codepen.io/kol730/pen/QqWBZz –

+0

請提供詳情。 – bojeil

+0

我剛剛在 –