Meteor roles not added to user

Hello,

Recently I’ve been trying to add roles to my users. I use the useraccount package and the role package.

I configured useraccount to call a function when a new user is created, using the postSignUpHook hook.

In the hook, everything seems to work since I can retrieve the user’s information. But the role is not added in the database. Yet the documentation ensures that this hook is called when a user is successfully created.

What am I doing wrong?

Here is my code:

"server/account.js"
let myPostSignUpHook = function (userId, info) {
    console.log("postSignupHook");
    console.log(userId);
    console.log(info);
    Roles.addUsersToRoles(userId, 'user', Roles.GLOBAL_GROUP);
}

AccountsTemplates.configure({
    homeRoutePath: '/',
    postSignUpHook: myPostSignUpHook,
});

PS: Roles are already created.
image

Use

Roles.setUserRoles(userId, 'user', Roles.GLOBAL_GROUP);

instead of

Roles.addUsersToRoles(userId, 'user', Roles.GLOBAL_GROUP);

1 Like

Thank you @bordalix for your answer but it does not work :neutral_face:

1 Like

Weird, that’s the example on https://github.com/Meteor-Community-Packages/meteor-roles

My code works with that.

Are you running v3? Are you running it on the server side?

1 Like

Ok I was stucked in v2 behavior ! I didnt’ see that I needed to add this in my server code:


Meteor.publish(null, function () {

    if (this.userId) {

        return Meteor.roleAssignment.find({ 'user._id': this.userId });

    } else {

        this.ready();

    }

});

And I was searching in wrong collection.
Ok my bad !

Thank you very much @bordalix

1 Like