2017-08-27 46 views
0

我有一個網絡應用程序,允許account.Once用戶已登錄用戶在使用他們的Gmail簽名,我能看到它的使用result.user對象的細節,如這個 -如何在使用Firebase的Web應用中的Google身份驗證之後獲取用戶詳細信息?

function signInWithGoogle(){ 

    var provider=new firebase.auth.GoogleAuthProvider(); 
    firebase.auth().signInWithPopup(provider).then(function(result){ 

     var user=result.user; 

     console.log("user_provider="+user.displayName+" user_email="+user.email+" user_dp="+user.photoURL+" user_verification="+user.emailVerified+" uid="+user.uid); 

    }).catch(function(error){ 
     console.log("error="+error); 
    }); 
} 

簽訂後在,我想保持頁面的用戶詳細信息,甚至像這個 -

$(document).ready(function(){ 
    var user=firebase.auth().currentUser; 
    console.log(user); 
}); 

重裝並刷新了,我使用的身份驗證(的用戶對象)之後,但它顯示用戶爲空,雖然我會在身份驗證看到用戶電子郵件地址在火力點的控制檯。

P.S.我也使用onAuthStateChanged而不是currentUser,但它仍然不起作用。

回答

0

onAuthStateChanged應該工作。你必須正確聽取它。

firebase.auth().onAuthStateChanged(function(user) { 
    if (user) { 
    // currentUser should be available now. 
    } else { 
    // No user logged in. 
    } 
}); 

請記住,該狀態存儲在單個主機源網絡存儲中。因此,如果您導航到具有不同域的頁面,則狀態不會傳播。

+0

意味着如果我在同一網站的不同頁面上使用onAuthStateChanged,那麼它會給用戶空值? –

+0

這很清楚。它必須是相同的域。您是否期望在example1.com上登錄並在example2.com上獲得相同的用戶? – bojeil

+0

不能從example1.com/page1.html登錄,並在example1.com/page2.html中獲取用戶詳細信息 –

相關問題