0

我正在寫一個基本網站,我希望用戶能夠使用谷歌登錄登錄到該網站。我試圖通過Firebase做到這一點,並迄今取得了成功的結果。在他們的文檔中,聲明用戶將被存儲在Firebase實時數據庫中的變量「auth」中,並且他們的電子郵件和用戶ID將可以從所述變量訪問。我已經遵循了文檔中的所有步驟,並使用他們的示例文件作爲指導。在我的帳戶控制面板上,所有登錄我網站的用戶都會記下他們的用戶名和電子郵件地址。但是,在實時數據庫選項卡下,情況並非如此,因爲無論用戶登錄多少次,數據庫都保持空白。這是一個主要問題,因爲我需要能夠訪問用戶的電子郵件地址才能執行諸如將其顯示在網頁上的東西。谷歌登錄用戶沒有出現在fireabse數據庫中

我寫的代碼如下:

<html> 
    <head> 
    <title>SEatAMRS auth test</title> 
    <script src="https://www.gstatic.com/firebasejs/3.2.0/firebase.js"></script> 
    <script type="text/javascript"> 

    // Initialize Firebase 
    var config = { 
    apiKey: "****************2XqEQSO9Z8vhPF5w", 
    authDomain: "*********-build.firebaseapp.com", 
    databaseURL: "https://***********-build.firebaseio.com", 
    storageBucket: "***********-build.appspot.com", 
    }; 

    firebase.initializeApp(config); 

    //Creatse a provider object 
    var provider = new firebase.auth.GoogleAuthProvider(); 

    firebase.auth().signInWithPopup(provider).then(function(result) { 
    // This gives you a Google Access Token. You can use it to access the Google API. 
    var token = result.credential.accessToken; 
    // The signed-in user info. 
    var user = result.user; 
    // ... 
}).catch(function(error) { 
    // Handle Errors here. 
    var errorCode = error.code; 
    var errorMessage = error.message; 
    // The email of the user's account used. 
    var email = error.email; 
    // The firebase.auth.AuthCredential type that was used. 
    var credential = error.credential; 
    // ... 
}); 

    //Sign out function 
    firebase.auth().signOut().then(function() { 
    // Sign-out successful. 
}, function(error) { 
    // An error happened. 
}); 

    </script> 

    </head> 

    <body> 
    <h1>Welcome to the Google Sign in Fucntionality Test!</h1> 
</html> 
+0

用戶信息不會自動存儲在數據庫中。它可用於您的代碼和數據庫的安全規則。如果您想將信息存儲在數據庫中,則可以在自己的代碼中進行此操作。請參閱https://stackoverflow.com/questions/14673708/how-do-i-return-a-list-of-users-if-i-use-the-firebase-simple-username-password以及我們遺留下來的這個例子docs:https://www.firebase.com/docs/web/guide/user-auth.html#section-storing –

回答

0

首先你得到了用戶的用戶名和密碼。然後,在html中驗證憑據,然後在firebase中使用登錄方法。

+0

此代碼爲用戶提供了一個彈出窗口,允許用戶輸入他們的Google帳戶憑據,從而驗證它們並允許它們通過其Google帳戶登錄到該網站。我添加的功能完全像它應該的那樣工作,除了用戶沒有被存儲在數據庫中的事實,因爲firebase文檔聲明他們應該這樣做。 – WebGavDev