简体   繁体   English

我是否需要将createUser代码放入流星方法中?

[英]Do I need to put createUser code in a meteor method?

I'm writing a meteor app and working on my user registration template. 我正在编写一个流星应用程序,并正在处理用户注册模板。

Currently I have the following code, imported on the client: 当前,我在客户端上导入了以下代码:

Template.register.events({
  'submit form': function(event){
    event.preventDefault();
    let username = $('[id=input-username').val();
    let email = $('[id=input-email]').val();
    let password = $('[id=input-password]').val();
    Accounts.createUser({
      username: username,
      email: email,
      password: password
    }, function(error){
      if(error){
        Bert.alert( "That username or email is either taken or invalid. Try again.", 'danger', 'growl-top-right' );
        // console.log(error.reason);
      }
      else {
        FlowRouter.go('mainLayout');
      }
    });
  }
});

My question is, is it ok to have the Accounts.createUser code on the client or do I need to call this from a meteor method imported on the server? 我的问题是,可以在客户端上使用Accounts.createUser代码,还是需要从服务器上导入的流星方法调用此代码? In my head I'm thinking a user can register as many times as they like with different emails / usernames therefore what's the harm in having the code on the client vs making a call to the server. 在我的脑海中,我认为用户可以使用不同的电子邮件/用户名注册任意多次,因此在客户端上存储代码与调用服务器相比有什么害处?

Thoughts welcome. 欢迎思想。

CreateUser is designed to be used from the client. CreateUser设计为可从客户端使用。 It handles the encryption of the password before it is sent to the server. 在将密码发送到服务器之前,它会处理密码的加密。

You can do validations at client side to save time but ideally you should write the code in meteor method on server side and call it on client side via Meteor.call(). 您可以在客户端进行验证以节省时间,但是理想情况下,您应该在服务器端的流星方法中编写代码,然后通过Meteor.call()在客户端调用它。 In your case I can simply add users using chrome console and can loop it to million times to add random stuff in your db. 在您的情况下,我可以简单地使用chrome控制台添加用户,并且可以将其循环到一百万次以在数据库中添加随机内容。 Csrf attacks are mostly welcome this way. 这种方式最受Csrf攻击。 You should also specify collections.allow() and collections.deny() when you are defining a new Mongo.Collection(). 定义新的Mongo.Collection()时,还应该指定collections.allow()和collections.deny()。 Also you should remove autopublish and insecure package from meteor project. 另外,您应该从流星项目中删除自动发布和不安全的软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM