无法在简单的HTML JS页面上使用Firebase身份验证进行身份验证

时间:2019-05-20 07:08:03

标签: javascript firebase firebase-authentication

在身份验证期间,Firebase似乎无法正常工作。这是我的html代码

    <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>

<body>

    <div class="login form">
        <input type="text" class="loginEmail" placeholder="Email..." id="email_field">
        <br><br><br>
        <input type="password" class="loginPass" placeholder="Password..." id="password_field">
        <br><br>
        <button class="logBut" onclick="login()">Log in</button>
    </div>

</body>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/6.0.2/firebase-app.js"></script>

<!-- TODO: Add SDKs for Firebase products that you want to use
     https://firebase.google.com/docs/web/setup#config-web-app -->
<!-- Private -->

</script>
<script src="index.js"></script>

</html>

我的index.js代码

    function login() {

    var userEmail = document.getElementById("email_field").value;
    var userPass = document.getElementById("password_field").value;

    firebase.auth().signInWithEmailAndPassword(userEmail, userPass).catch(function (error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;

        window.alert("Error : " + errorMessage);

        // ...
    });

}

我不想使用React或Angular等前端框架。这应该是相当简单的,因为这段代码前一阵子起作用了,但是现在不起作用了

1 个答案:

答案 0 :(得分:1)

添加以下脚本

<!-- Add Firebase products that you want to use -->
<script src="https://www.gstatic.com/firebasejs/6.0.2/firebase-auth.js"> 
</script>
<script src="https://www.gstatic.com/firebasejs/6.0.2/firebase-firestore.js"> 
</script>

初始化您的Firebase配置

var firebaseConfig = {
  apiKey: "api-key",
  authDomain: "project-id.firebaseapp.com",
  databaseURL: "https://project-id.firebaseio.com",
  projectId: "project-id",
  storageBucket: "project-id.appspot.com",
  messagingSenderId: "sender-id",
  appID: "app-id",
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);

在运行示例之前,请替换您的应用程序凭据。