smallguyxoxo
smallguyxoxo

Reputation: 71

Cannot authenticate using firebase authentication on a simple HTML, JS page

Firebase doesn't seems to work while authentication. This is my html code

    <!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>

My index.js code

    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);

        // ...
    });

}

I don't want to use frontend frameworks like React or Angular. This should be fairly simple as this code worked a while ago but is not working now

Upvotes: 0

Views: 381

Answers (1)

Santosh
Santosh

Reputation: 2333

Add the following scripts

<!-- 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>

Initialise your firebase config

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);

Replace your app credentials before you run your sample.

Upvotes: 1

Related Questions