【问题标题】:Meteor: check email does not exist in Meteor.users collection before creating an accountMeteor:在创建帐户之前检查 Meteor.users 集合中不存在电子邮件
【发布时间】:2018-09-17 14:21:20
【问题描述】:

我有一个表单,用户可以在其中输入他们的电子邮件地址和密码到加入表单中。这将创建帐户,但我现在想进一步开发它。

client.js:

Template.joinForm.events({
    'submit .form-join': function(e, t) {
        e.preventDefault();
        var email = t.find('#email').value,
        password = t.find('#password').value,
        username = Random.id(),
        array = [],
        profile = {
            nameOfArray: array
        };
        Accounts.createUser({
            email: email,
            username: username,
            password: password,
            profile: profile
        }, function(error) {
            if (error) {
                alert(error);
            } else {
                Router.go('/');
            }
        });
    }
});

创建用户帐户之前,您如何:

  1. 检查 joinForm 中的 email 变量是否不存在于 Meteor.users 集合中。在服务器上处理这个?

  2. 如果email确实存在,那么拒绝用户创建?

我看到了新功能,想知道我是否可以使用这个http://docs.meteor.com/#/full/accounts_validatenewuser

Accounts.validateNewUser(function (user) {
    // pseudo if statement code
    if (email not exist) {
        // 1. create the user account and then
        Accounts.sendVerificationEmail(userId, [email])
    } else {
        throw new Meteor.Error(403, "email address is already registered");
    }
});

感谢您阅读本文。

我不清楚是使用Accounts.createUser 还是Accounts.onCreateUser,哪些代码应该在客户端,哪些在服务器。我的目标是安全地建立帐户,因此,在此过程中从控制台拒绝任何其他修改权限。

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    现在在服务器上创建额外的空数组nameOfArray如果允许创建帐户,即传递validateNewUser 函数。当然,您可以添加更多验证检查,例如密码长度。

    client.js:

    Template.joinForm.events({
        'submit .form-join': function(e, t) {
            e.preventDefault();
            var email = t.find('#email').value,
                password = t.find('#password').value,
                username = Random.id();
            Accounts.createUser({
                email: email,
                username: username,
                password: password,
                profile: profile
            }, function(error) {
                if (error) {
                    alert(error.reason);
                } else {
                    Router.go('/');
                }
            });
        }
    });
    

    server.js:

    Accounts.onCreateUser(function(options, user) {
        var newEmail = user.emails[0].address;
        console.log(newEmail);
        var emailAlreadyExist = Meteor.users
            .find({"emails.address": newEmail}, {limit: 1})
            .count() > 0;
        console.log(emailAlreadyExist + ' already exists');
        if (emailAlreadyExist === true) {
            throw new Meteor.Error(403, "email already registered");
        } else {
            profile = options.profile;
            profile.nameOfArray = [];
            user.profile = profile;
            return user;
        }
    });
    

    【讨论】:

    • 能否解释一下这部分profile = options.profileprofile.nameOfArray =[]user.profile = profile
    【解决方案2】:

    我发现Accounts.createUser 内置了自己的验证功能,并检查现有的电子邮件/登录信息。

    Meteor docs: Accounts.createUser:

    如果现有用户的用户名或电子邮件仅在 情况下,createUser 会失败。

    因此,如果Accounts.createUser 电子邮件/登录验证抛出错误,Accounts.onCreateUser 甚至不会触发。

    【讨论】:

      【解决方案3】:

      Accounts.validateNewUser 功能要求用户在提交后验证其电子邮件。基本上就是这样一个步骤,在您注册某项内容后,您必须在您的电子邮件或移动设备上输入发送给您的代码 - 基本上,它确保用户是他们所说的那个人。这可能会阻止您使用电子邮件totallyfake@totally_not_a_real_place.com 注册。

      如果我没看错您的问题,那么您更感兴趣的是查看电子邮件是否是唯一的,而不是查看用户是否真正拥有该电子邮件帐户。您可以使用在服务器上运行的Accounts.onCreateUser 来执行此操作:

      每当创建新用户时调用。返回新的用户对象,或抛出错误以中止创建。

      整个过程看起来像这样。在客户端,正是你所拥有的:

      Template.joinForm.events({
      'submit .form-join': function(e, t) {
          e.preventDefault();
          var email = t.find('#email').value,
              password = t.find('#password').value,
              username = Random.id(),
              array = [],
              profile = {
                  nameOfArray: array
              };
      
          Accounts.createUser({
              email: email,
              username: username,
              password: password,
              profile: profile
          }, function(error) {
              if (error) {
                  alert(error);
              } else {
                  Router.go('/');
              }
          });
          }
      });
      

      然后,在服务器上,在实际创建用户之前,它将通过您的 onCreateUser 函数运行用户,并且您返回的任何内容都将插入到用户集合中:

      Accounts.onCreateUser(function(options, user) { 
        var email = user.emails[0];
        if (!email) { throw new Meteor.Error("You must provide a non-empty email"); // this may actually not be necessary -- createUser might do it for you
        if (Meteor.users.find({emails: email}) { 
          throw new Meteor.Error("A user with email " + email + " already exists!"); 
        }
      
        ... // whatever other transformation you might want
        return user; 
      });
      

      您还可以查看accounts-ui package,因为根据您想要做的事情与普通用户帐户的实现不同,您可能已经完成了很多工作。

      【讨论】:

      • 卡森,你是对的。验证需要在 Accounts.onCreateUser 中完成。
      【解决方案4】:

      Accounts.validateNewUser 用于检查用户对象的字段是否符合所需的格式,并相应地返回 true 或 false。

      要检查电子邮件是否已经注册,我认为您应该在 Accounts.onCreateUser 函数 (http://docs.meteor.com/#/full/accounts_oncreateuser) 中包含此验证。

      在没有测试过代码的情况下,你可以尝试这样的事情:

      Accounts.validateNewUser(function (user) {
           // Check compliance of the user object fields, using check http://docs.meteor.com/#/full/check
      });
      
      Accounts.onCreateUser(function(options, user) {
             if (options.profile){
                user.profile = options.profile;
             }
      
             if (Meteor.users.find({email: user.email}).fetch == 0) {
                 if(Accounts.validateNewUser(user)){
                      Accounts.sendVerificationEmail(userId, [email]);
                      return user;
                 } else {
                     throw new Meteor.Error(403, "Error checking user fields");
             } else {
               throw new Meteor.Error(403, "email address is already registered");
             }       
       }     
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-17
        • 2019-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-05
        相关资源
        最近更新 更多