MaximusDecimus
MaximusDecimus

Reputation: 13

Meteor's createUser running on client and server

I'm fairly new to Meteor and trying to grasp its concepts. I have a client code below that triggers Meteor method to create new user:

Template["signup-team"].onRendered(function(){
    var validator = $('.signup-team-form').validate({
        submitHandler: function(event){
            var email = $('[name=email]').val();
            var password = $('[name=password]').val();
            Meteor.call('addNewUser', email, password, "team-captain", function(error, result) {
                if (error){
                    return alert(error.reason);
                }
                Router.go("complete-signup");
            });
        }
    });
});

The method is defined to run on both client and server. When run on the server I want it to create user and add role to account. On the client side I want to sign user in.

Meteor.methods({
    addNewUser: function(email, password, role) {
        check(email, String);
        check(password, String);

        if(Meteor.isClient){
            Accounts.createUser({
                email: email,
                password: password,
                profile: {
                    completed: false
                }
            }, function(error){
                if(error){
                    console.log(error); // Output error if registration fails
                } else {
                    console.log(Meteor.userId());
                }
            });
        } else {
            var id = Accounts.createUser({
                email: email,
                password: password,
                profile: {
                    completed: false
                }
            });
            console.log(id);
            Roles.addUsersToRoles(id, role);            
        }
    }
});

The server part runs fine and new user is created but on client side I get error Error: No result from call to createUser and user isn't signed in automatically.

I assume the problem is I dont need to run createUser on the client and use Meteor.loginWithPassword instead but I would really like to know the theory behind this. Thanks

Upvotes: 1

Views: 417

Answers (1)

JeremyK
JeremyK

Reputation: 3240

Don't do this. You are rewriting core code and creating security issues needlessly.

Instead of using your addNewUser method, just call Accounts.createUser on the client. Have a onCreateUser callback handle adding the role.

In your code, you are sending the users password to the server in plaintext. When you call Accounts.createUser, the password is hashed before being sent to the server. It also takes care of logging in the new user for you.

One gotcha with adding the role though, you will not be able to use Roles.addUsersToRoles(id, role) in the onCreateUser callback, as the user object has not yet been added to the database, and does not have an _id. However you can directly add the role to the proposed user object like this:

Accounts.onCreateUser(function(options, user) {
  user.roles = ['team-captain']  
  return user;
})

Then again, maybe you don't want all users to be team captains!

Upvotes: 1

Related Questions