验证用户并同时添加它们

时间:2017-02-03 21:31:32

标签: javascript authentication firebase

我想注册新用户(通过身份验证),然后将它们(及其名称和其他信息)添加到实时数据库中的用户列表数据库中。我无法弄清楚我做错了什么。身份验证效果很好,但新用户未添加到数据库中。

    var fname = document.getElementById('fname').value;
    var lname = document.getElementById('lname').value;
    var email = document.getElementById('email').value;

在下面的代码中,我注册了它们,然后将它们的名字添加到数据库中,然后发送验证邮件。

function handleRegister() {
     var ref = firebase.database().ref();
     console.log(email);
     console.log(fname);

        if (email.length < 4) {
        alert('Please enter an email address.');
        return;
      }
      if (password.length < 4) {
        alert('Please enter a password.');
        return;
      }

      firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {


      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      var uid = firebase.auth().currentUser.uid; 
      // [START_EXCLUDE]
      if (errorCode == 'auth/weak-password') {
        alert('The password is too weak.');

             firebase.auth().onAuthStateChanged(user => {
        if(user) {

              var postData = {
                Fullname: fname + lname,
                  email: email,
              };

              // Write the new post's data simultaneously in the posts list and the user's post list.
              var updates = {};
              updates['/Users/' + uid ] = postData;

              return firebase.database().ref().update(updates);

        }
      })
      } else {
         console.log(error);

    }

  })

身份验证和发送电子邮件验证工作正常但名称未添加到数据库中。此外,如果有更好的方法来实现身份验证,添加到数据库并发送电子邮件验证,请告诉我。请帮忙。

这是更新后的补充

var addusertoDB = function(user){

           var uid = firebase.getAuth().uid;
           var postData = {
            Firstname: fname,
            Lastname: lname,
            email: email,
          }
            // Get a key for a new Post.
          var newPostKey = firebase.database().ref().child('Users').push().uid
          // Write the new post's data simultaneously in the posts list and the user's post list.
          var updates = {};
          updates['/Users/' + newPostKey] = postData;
          // updates['/user-posts/' + '/' + newPostKey] = postData;
          return firebase.database().ref().update(updates);
    }

并且句柄寄存器已更新为

  firebase.auth().createUserWithEmailAndPassword(email, password).then(


        addusertoDB).catch(handleCreateUserError);

它最终被添加到DB(没有uid)但是firebase.getAuth()。uid没有得到uid。我得到的错误是&#34; firebase.getAuth不是函数&#34;

1 个答案:

答案 0 :(得分:1)

您正尝试在传递给catch()的同一函数中处理错误和用户更新。这意味着该函数内的任何代码仅在firebase.auth().createUserWithEmailAndPassword(email, password)失败时运行。

来自firebase文档:

  

<强> createUserWithEmailAndPassword

     

createUserWithEmailAndPassword(email, password)返回   firebase.Promise包含非空firebase.User

     

创建与指定电子邮件地址关联的新用户帐户   和密码。

     

成功创建用户帐户后,该用户也将成为   已登录您的申请。

这意味着在成功创建用户后,您可以通过传递给then()的回调来访问新用户。

你可能想要这样的东西:

var doSomethingWithNewUser = function(user) {
    // Manipulate the newly created User however you like here.
    // You don't have to sign them in again.
};

var handleCreateUserError = function(error) {
    var errorCode = error.code;
    var errorMessage = error.message;
    // Do whatever you want with the error codes.
};

firebase.auth().createUserWithEmailAndPassword(email, password)
    .then(doSomethingWithNewUser)
    .catch(handleCreateUserError);