Search code examples
javascriptmeteormeteor-accounts

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? 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.


Solution

  • CreateUser is designed to be used from the client. It handles the encryption of the password before it is sent to the server.