Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

function not working on "submit" event, but works when ran in dev console

I am rendering the form below, and below that I have a script. When I load the page, type in the email and password, then submit, it appears to just reload the page (regardless of credentials).

When I type in the email and password, and then execute the function in the dev console by typing loginUser() it executes as expected and if creds are correct it redirects me as it should. If credentials are wrong the alert events fire.

I have put in breakpoints, and on "submit" it gets the email and password that are in the form, but then seems to jump back up to the start of the function (regardless of credentials).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let form = document.getElementById('loginform');
form.addEventListener('submit', loginUser);

function loginUser() {
  let email = document.getElementById('email').value;
  let password = document.getElementById('password').value;
  firebase.auth().signInWithEmailAndPassword(email, password).then(() => {
    window.location.href = "/";
  }).catch(error => {
    console.log(error)
    let errorCode = error.code;
    let errorMessage = error.message;
    if (errorCode == 'auth/wrong-password') {
      window.alert('Entered password was incorrect.');
    } else if (errorCode == 'auth/user-not-found') {
      window.alert('Entered email is invalid.');
    } else {
      window.alert('Please check your login credentials.');
    }
  });
};
firebase.auth().onAuthStateChanged(user => {
  if (user) {
    console.log(user)
    user.getIdToken().then(function(token) {
      //save the token in a cookie  
      document.cookie = "token=" + token;
    });
  } else {
    console.log("What up dude?");
  }
});
<form id="loginform" action="" method="post">
  <div class="form-group">
    <label for="email">Email</label> <input class="form-control" id="email" name="email" required="required" type="text" value="">
  </div>
  <div class="form-group">
    <label for="password">Password</label> <input class="form-control" id="password" name="password" required="required" type="password" value="">
  </div>
  <div class="form-group">
    <input class="btn btn-primary" id="submit" name="submit" type="submit" value="Login">
  </div>
</form>

>Solution :

It is imperative to REMOVE or RENAME id="submit" name="submit"

Calling anything submit in a form hides the submit method

Then add preventDefault() to stop submission:

function loginUser(e) {
  e.preventDefault()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading