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