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