Am using meteor-useraccounts and alanning:roles in my Meteor webapp. The idea is that a user can register and automatically gets assigned a role. So I was thinking of using the Accounts.onCreateUser hook, however can't get that to work.
Accounts.onCreateUser(function(options, user) {
var user = user.user;
var defaultRole = ['admin'];
if (!user.roles){
Roles.addUsersToRoles(user, defaultRole);
}
});
I'm getting the following error message:
Exception while invoking method 'ATCreateUserServer' TypeError: Cannot read property 'roles' of undefined
Seems that the user is not known, hence undefined. Can you user Meteor-useraccounts and assign a role upon user creation.
NEW EDIT: tried a bit further and the below code is working but I'm not really in favor of using this as it is strange to add the role each time upon login of the user:
Accounts.onLogin(function(user) {
check(user, Object);
var user = user.user;
var defaultRole = ['admin'];
if (!user.roles){
Roles.addUsersToRoles(user, defaultRole);
}
return user;
});