Firebase身份验证无效

时间:2017-08-10 11:23:21

标签: javascript html html5 firebase firebase-authentication

我一直在使用Firebase进行身份验证,只要有两种警报方法,我就能让它工作:

var x;
var y; 

function boo() {
   if (document.getElementById('email') !== null && document.getElementById('password') !== null) {
      x = document.getElementById("email").value;
      y = document.getElementById('password').value;
      alert(x);
      alert(y);
      const auth = firebase.auth();
      auth.createUserWithEmailAndPassword(x, y);
      } else {
            ...
      }
}

但是,如果我删除了两种警报​​方法,则Firebase中不会发送任何内容,也不会在控制台中看到任何错误。我希望有人知道发生了什么,因为每次有人报名时我都不想显示电子邮件和密码。

这是HTML:

<form class="signupForm">
    <input type="email" class="inputs" onblur="validateForm()" id="email" required name="email" placeholder="Email" />
    <input type="password" class="inputs" id="password" required name="password" placeholder="Password" />
    <button id="btn" class="buttons" onclick="boo()">Signup</button>
</form> 

2 个答案:

答案 0 :(得分:1)

<form>标记替换为<div>

使用此代码检查错误:

auth.createUserWithEmailAndPassword(email, 
password).then(function(user) {
   var user = firebase.auth().currentUser;
   console.log(user); // Optional
}, function(error) {
   // Handle Errors here.
   var errorCode = error.code;
   var errorMessage = error.message;
});

检查您的firebase配置信息:

  // Initialize Firebase
  // TODO: Replace with your project's customized code snippet
  var config = {
    apiKey: "<API_KEY>",
    authDomain: "<PROJECT_ID>.firebaseapp.com",
    databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
    storageBucket: "<BUCKET>.appspot.com",
    messagingSenderId: "<SENDER_ID>",
  };
  firebase.initializeApp(config);

同样在firebase控制台中 - &gt;身份验证,检查登录方法电子邮件/密码是否已启用

enter image description here

答案 1 :(得分:0)

使用event.preventDefault,如下所示:

async function handleSubmit(event) {
    event.preventDefault();
    const email = event.target.elements.email.value;
    const password = event.target.elements.password.value;
    // alert(email)
    // alert(password)
    await createNewUserWithFirebase(email, password);
}