[SOLVED]How to add a role to this user

Hey I need help to assign a role when im running this code:

var accountInfo = {
                email: email,
                password: password,
                profile: {
                    firstname : firstname,
                    lastname: lastname,
                    adress: adress,
                    zip: zip,
                    date: date,
                    phone: phone}
            };
             var user = Accounts.createUser(accountInfo, function (er) {
                if (er){
                    alert(er.reason);
                    console.log(er.reason)
                } else {
                    FlowRouter.go('/');
                }
            })
        } else {
            alert("Passordene stemmer ikke");
        }

I would like all users to get registered here to be assigned the “user” role. I’ve tried different things with Allanning:roles, but I can’t get it to work somehow. Also is Accounts.createUser safe to use on client side or should I use it on the server side ?

Look at Accounts.onCreateUser
You can add everything you want…

And… never trust users (-:

Tried to do it now, but the user wont be stored to the role. I can see that the role is created in MongoDB, but user is not connected to the role somehow. This is the code im running on the server:

Accounts.onCreateUser(function (options, user) {
    if (options.profile){
        user.profile = options.profile;
        Roles.addUsersToRoles(user._id, 'user', Roles.GLOBAL_GROUP);
    }
    return user;
});

You have to manually set the role, without Roles

did you check ur user db it dont have the role field??

const checkIfFirstUser = () => {
  const userCount = Meteor.users.find().count();
  return userCount === 0;
};

const assignRoles = (options, user) => {
  const firstUser = checkIfFirstUser();
  if (firstUser) {
    const roles = ['admin'];
    user.roles = roles;
    Roles.addUsersToRoles(user._id, roles);
  } else {
    const roles = ['user'];
    user.roles = roles;
    Roles.addUsersToRoles(user._id, roles);
  }
  return user;
};

Accounts.onCreateUser((options, user) => {
  if (options.profile) {
    user.profile = options.profile;
  }
  assignRoles(options, user);
  return user;
});

Hi @matwii, the alanning:roles readme docs have an example of how to do this. You can find it here: https://github.com/alanning/meteor-roles#usage-examples

Here is the example:

var users = [
      {name:"Normal User",email:"normal@example.com",roles:[]},
      {name:"View-Secrets User",email:"view@example.com",roles:['view-secrets']},
      {name:"Manage-Users User",email:"manage@example.com",roles:['manage-users']},
      {name:"Admin User",email:"admin@example.com",roles:['admin']}
    ];

_.each(users, function (user) {
  var id;

  id = Accounts.createUser({
    email: user.email,
    password: "apple1",
    profile: { name: user.name }
  });

  if (user.roles.length > 0) {
    // Need _id of existing user record so this call must come
    // after `Accounts.createUser` or `Accounts.onCreate`
    Roles.addUsersToRoles(id, user.roles, 'default-group');
  }

});

Note that the Roles.addUsersToRoles call needs to come after Accounts.createUser or Accounts.onCreate or else the roles package won’t be able to find the user record (since it hasn’t been created yet). This SO answer gives more detail: http://stackoverflow.com/a/22650399/219238

Thanks for all the replies guys, but I have managed to solve the problem. The problem was that I had to assign a role to a user before using the Role package.