Firebase身份验证

时间:2016-07-22 18:08:59

标签: firebase firebase-authentication

不确定此代码为何无法运行。从Firebase复制了所有必需的代码段,我的按钮正在触发。关于什么可能出错的任何想法? Github让我在这里写更多东西。

谢谢!

<head>
    <meta charset=utf-8 />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Auth</title>
    <script src="https://www.gstatic.com/firebasejs/3.1.0/firebase.js"></script>
    <script src="https://www.gstatic.com/firebasejs/3.1.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/3.1.0/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/3.1.0/firebase-database.js"></script>
</head>

<body>

  <button onclick="googleAuth()">SIGN UP</button>

<script>

  var provider = new firebase.auth.GoogleAuthProvider();

  var googleAuth = function() {
    console.log(firebase);
    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;
      // ...
    });
};


</script>








  <script>
    // Initialize Firebase
    var config = {
      apiKey: "AIzaSyBeJIaAAD5-vK0kBDTRH6Y6_1ByVE3erJY",
      authDomain: "fir-chat-d28be.firebaseapp.com",
      databaseURL: "https://fir-chat-d28be.firebaseio.com",
      storageBucket: "fir-chat-d28be.appspot.com",
    };
    firebase.initializeApp(config);
  </script>

</body>

4 个答案:

答案 0 :(得分:2)

您必须按照日志中的说明进行操作:

The current domain is not authorized for OAuth operations. This will prevent signInWithPopup, signInWithRedirect, linkWithPopup and linkWithRedirect from working. Add your domain to the OAuth redirect domains list in the Firebase console -> Auth section -> Sign in method tab.

如果没有在Firebase控制台中添加您的域

,请查看signInWithPopup如何工作

答案 1 :(得分:0)

按照我遇到类似错误时列出的说明,这将使您能够解决oAuth问题。

Firebase GAS webapp Google popup disappears

答案 2 :(得分:0)

您的代码看起来不错,但如果您将其作为HTML文件运行,则会收到类似于“此应用程序正在运行的环境中不支持此操作的错误。”location.protocol“必须是http或者HTTPS”。

我建议你利用Firebase Hosting功能。您可以轻松部署和托管应用程序的静态资产,如HTML,CSS,JavaScript等。此外,它是免费的。

答案 3 :(得分:0)

根据我的经验(仅供咨询),不要使用firebase auth与google或facebook等提供商。使用电子邮件和密码验证以提高安全性。你可以使用FirebaseUi,易于理解,如果你愿意,你可以复制粘贴。如果您使用Google身份验证,则拥有Google帐户的每个用户都可以访问您的应用,进行读取,写入,删除和更新。除了,您有更好的使用Firebase规则的经验。