Raskayu
Raskayu

Reputation: 743

Firebase auth error auth/network-requested-failed

I'm using firebase Hosting to host my web, I try to login with email/pwd but this error prompts all the time.

This is the JS I use:

window.onload = () => initApp(); //Initiate screen

  function initApp(){
    console.log(firebase);

    document.getElementById("btnLogin").addEventListener("click", e => {
      var email = document.getElementById("inputEmail").value;
      var pwd = document.getElementById("inputPassword").value;

      firebase.auth().signInWithEmailAndPassword(email, pwd).catch(error => {
                          var errorCode = error.code;
                          var errorMessage = error.message;
                          console.log(errorCode + "-" + errorMessage);
                          alert(errorCode + "-" + errorMessage);
      }); //Login
    });//Login button click

    firebase.auth().onAuthStateChanged(user => {
      console.log("User: " + user);
      if (user) {
        // User is signed in.
        var displayName = user.displayName;
        var uEmail = user.email;
        var emailVerified = user.emailVerified;
        var photoURL = user.photoURL;
        var isAnonymous = user.isAnonymous;
        var uid = user.uid;
        var providerData = user.providerData;
        console.log(uEmail + " logged in");
        alert(uEmail + " logged in");
      } else {
        console.log("not logged in");
        alert("not logged in");
      }//Check user
    });//Check auth state changes        
  }//InitApp   

And this the HTML for the form:

<form id="formLogin" class="form-signin">
    <h2 align="center" class="form-signin-heading">Inicia sessió</h2>
    <label for="inputEmail" class="sr-only">Correu electrònic</label>
    <input type="email" id="inputEmail" class="form-control" placeholder="Correu electrònic" required autofocus>
    <label for="inputPassword" class="sr-only">Contrassenya</label>
    <input type="password" id="inputPassword" class="form-control" placeholder="Contrassenya" required>        
    <button id="btnLogin"class="btn btn-lg btn-primary btn-block" type="submit">Entrar</button>
  </form>

So, whats the problem here? If I use this JS code on Chrome/Firefox console it logs in and works.

EDITED: Somehow if I change the form tag to div it works, but it makes no sense. Is there any issue not know about using html5 forms with firebase?

Upvotes: 1

Views: 3433

Answers (1)

CodingIntrigue
CodingIntrigue

Reputation: 78525

The default behaviour of a <button> inside a <form> element is to submit the form. This is causing your page to reload & the Firebase login code to be cancelled.

This is why it works when you change to a <div> element.

You need to use preventDefault in your button listener to prevent this behaviour:

document.getElementById("btnLogin").addEventListener("click", e => {
      e.preventDefault(); // Stop the form from submitting
      var email = document.getElementById("inputEmail").value;
      var pwd = document.getElementById("inputPassword").value;

      firebase.auth().signInWithEmailAndPassword(email, pwd).catch(error => {
                          var errorCode = error.code;
                          var errorMessage = error.message;
                          console.log(errorCode + "-" + errorMessage);
                          alert(errorCode + "-" + errorMessage);
      }); //Login
    });//Login button click

Upvotes: 4

Related Questions