首页 > 解决方案 > Firebase signInWithEmailAndPassword not working as well

问题描述

I'm getting problems when try to login with an registered email in signInWithEmailAndPassword Firebase method in JavaScript. I know that the problem is with the fields required, but i don't know how to fix it. Here is my snipet

        <form>
          <input type="email" id="inputEmail" class="form-control" placeholder="Email address" autofocus="autofocus" required="required">
          <input type="password"  class="form-control" id="inputPassword" placeholder="Password">
          <button class="btn btn-primary btn-block"     id="btnLogin">Login</button>
        </form>

  <script>
  $('#btnLogin').click(function(){

  var email = $('#inputEmail').val();
  var password = $('#inputPassword').val();

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

        if (errorCode === 'auth/wrong-password') {
          alert('Wrong password.');
        } else {
          alert(errorMessage);         
        }
            console.log(error);
    }); 
});

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
    alert("User is signed in");
  } else {
    // No user is signed in.
    alert("No user is signed in");
  }
});
</script>
</body>

This was to be the original code that does not work (it gives me the message "No user is signed in"), but if I force a validation error (for example leave the fields empty and put the value directly in the variables), it gives seconds and returns the message "User is signed in"

var email = "a@a.com";
var password = "123456";

//var email = $('#inputEmail').val();
//var password = $('#inputPassword').val();

Another way I forced to give the error and it worked was putting an input hidden and required. In that case I returned an error in the console, but after a few seconds I received the message "User is signed in". The code of input:

<input type="text" class="form-control" id="inputHidden" required="required" hidden>

The error:

An invalid form control with name='' is not focusable.

ReferenceError: email is not defined
at Object.next (login.html:135)
at subscribe.ts:104
at subscribe.ts:225

标签: javascripthtmlfirebasefirebase-authentication

解决方案


推荐阅读