javascript - 验证用户身份并同时将其添加到数据库

标签 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);

它最终被添加到数据库(没有 uid),但 firebase.getAuth().uid 没有获取 uid。我收到的错误是“firebase.getAuth 不是函数”

最佳答案

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

来自 firebase 文档:

createUserWithEmailAndPassword

createUserWithEmailAndPassword(email, password) returns firebase.Promise containing non-null firebase.User

Creates a new user account associated with the specified email address and password.

On successful creation of the user account, this user will also be signed in to your application.

这意味着成功创建用户后,您将可以通过传递到 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);

关于javascript - 验证用户身份并同时将其添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42033316/

相关文章:

Django APIClient 登录不起作用

firebase - 如何在 Firebase 应用中获取 Cloud Storage 中所有文件的列表?

javascript - ReactJS 中的 session 计时器

javascript - amcharts 获取鼠标的实际 xValue(日期)

javascript - 如何修复垂直堆叠的图像水平卷轴滚动幻灯片图像?

javascript - 我想将 Firebase 中的数据检索到 HTML 文本框中,然后更新 Firebase 中的数据

javascript - 如何通过 index.html 在 Firebase 存储中显示图像

javascript - 如何从 then 和 catch block 中捆绑一个 Promise

php - Joomla 登录页面,如果用户已经登录,则重定向到主页

python - 使用Requests登录jsp网站