2015-03-25 70 views
2

我有一个表单,用户在连接表单中输入他们的电子邮件地址和密码。这创建了帐户,但我现在想要进一步开发它。流星:创建帐户之前,Meteor.users收集中不存在检查邮件

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“电子邮件”变量尚不存在的流星。用户收集(在服务器上处理)?

  1. 如果'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和 应该是客户上的代码,其中服务器上。我的目标是安全地建立帐户,因此,在此过程中从控制台拒绝任何其他修改权限。

回答

0

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"); 
     }  
}  
1

Accounts.validateNewUser功能需要用户提交后,以验证他们的电子邮件。这基本上就是在注册某件事物之后,在您登录之前,您必须输入通过电子邮件或移动设备发送给您的代码 - 基本上,可以确保用户是他们自称的用户。这可能会阻止您使用电子邮件[email protected]_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,因为这取决于你想要多少就从香草实现用户帐户的变化,很多可能已经为你做。

+0

卡森,你是对的。验证需要在Accounts.onCreateUser中完成。 – meteorBuzz 2015-03-26 18:36:16

5

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

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 
    } 

}) 
+0

你能解释一下这部分'profile = options.profile'' profile.nameOfArray = []''user.profile = profile'吗? – Edgar 2016-02-28 23:36:39

3

我发现Accounts.createUser有它的建成和检查已有的电子邮件/登录自己的验证。

Meteor docs: Accounts.createUser:

如果有一个用户名或电子邮件只在 不同的情况下,现有用户的createUser将失败。

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