Firebase signInWithEmailAndPassword效果不佳

时间:2019-06-05 20:59:53

标签: javascript html firebase firebase-authentication

尝试使用JavaScript中的signInWithEmailAndPassword Firebase方法中的已注册电子邮件登录时遇到问题。我知道问题出在必填字段,但我不知道如何解决。这是我的摘录

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

这是无效的原始代码(它向我显示消息“没有用户登录”),但是如果我强制执行验证错误(例如,将字段留空并将值直接放在变量),则给出秒数并返回消息“用户已登录”

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

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

另一种我不得不给出错误并且有效的方法是将输入隐藏起来并且是必需的。在那种情况下,我在控制台中返回了一个错误,但是几秒钟后,我收到了消息“用户已登录”。输入代码:

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

错误:

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

0 个答案:

没有答案
相关问题