Question

my first button creates a simple user like this:

'click .insertBtn': function()
    {
        var email = document.getElementById('email').value;
        var pass = document.getElementById('password').value;

        Accounts.createUser(
        {
            email: email,
            password: pass,
            profile: { name: 'test' }
        }); 

        Roles.addUsersToRoles([this._id], ['admin']);
    }

This user gets assigned a role of "admin". I then while I am logged in have a button to check if I am admin:

'click .checkRole': function()
    {
        console.log(Roles.userIsInRole(Meteor.userId(), 'admin'));
    }

But it is always returning false, so what may be the problem?

The id of the user and the role are different, is that how it should be?

Was it helpful?

Solution

The user may not have successfully been added to the role. The thing is you really use this line of code on the client side (Because then anyone would just type that in to themselves and become admin):

Roles.addUsersToRoles([this._id], ['admin']);

You have to do this process on the server, perhaps in the Accounts.onCreateUser callback on the server side of your project.

Or you could connect to your meteor's mongodb and add a roles: ['admin'] field to your user's document in the users collection if this is something once off.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top