验证电子邮件(js)后,Firebase会执行一次操作

时间:2018-09-23 19:27:16

标签: javascript firebase email-verification

我正在尝试向我的Firebase注册添加电子邮件验证, 问题是我还需要使用新的用户ID在数据库中创建一个节点,并且为此,我使用了这两行代码(在createUserByEmailAndPassword内部):

var uid = user.uid;
database.ref().child('users').child(uid).set(uid);

被调用一次,因为它位于创建对象内部。

但是现在通过添加的电子邮件验证过程,我不确定应该将其放置在何处。在我当前的代码下面(没有上述几行):

// Sign up with email
btnSignup.addEventListener('click', e => {

  const email = txtEmail.value;
  const pass = txtPassword.value;
  const auth = firebase.auth();
  var user = firebase.auth().currentUser;
  var verif = user.emailVerified;

  const promise = auth.createUserWithEmailAndPassword(email, pass);
  promise.then(user => {
    user.sendEmailVerification();
  }).catch(function(error) {
  var errorCode = error.code;
  var errorMessage = error.message;
  alert('Error: ' + errorMessage);
  });

});

// Add a realtime listener
firebase.auth().onAuthStateChanged(firebaseUser => {
  if (firebaseUser.emailVerified) {
    console.log('Email is verified');
    console.log(user);
    $('#loginBox').modal('hide');
    userIsLogged = true;
  } else {
    userIsLogged = false;
    window.location = "index.html";
    firebase.auth().signOut();
    alert("Email is not verified");
  }
});

0 个答案:

没有答案