Adding a Role on Creating User

Hello there.
Using alanning:roles for group management, and last version of everything.

I was following the tutorial on (Official Repository)[], trying to add the role to the user like this:

// server/main.js
Accounts.onCreateUser(function (options, user) {
        Roles.addUsersToRoles(user._id, ['Convidado']);
        if (! {

But this did not work.
When I did this, the user was created with the correct role:

Accounts.onCreateUser(function (options, user) {
        user.roles = ['Convidado'];
        if (! {

Am I doing it wrong? I think I might doing some kind of mistake here, and breaking server side verification.
Another question:
What does the best practice says about onCreateUser location? Should I put this routine another place?

Thanks in advance.

Reading over the docs I found this…

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:

I would personally add matb33:collection-hooks and then use an after.insert hook on the users collection.


Got it. Thanks.

Any ideia on why the second method worked?

Accounts.onCreateUser happens prior to inserting the user into the database and so the user document doesn’t have an _id yet.

1 Like