Roles not being recognized

Trying to follow instructions for alanning:roles but to no avail. I’m able to create users, but the helper check {{#if isInRole ‘client’}} doesn’t recognize the user being in that role, which I am. Follow up questions would be appreciated and I’ll try and respond as soon as I’m able to. Thanks!

// fixtures.js

function createUsers() {
	let users;

	if (Meteor.users.find().fetch().length === 0) {
		console.log('Creating users: ');

		users = [
			{
				name: 'user_clayton',
				email: 'user_clayton@example.com',
				roles: ['user'],
			},
			{
				name: 'client_clayton',
				email: 'client_clayton@example.com',
				roles: ['client'],
			},		
		];

		users.forEach((userData) => {
			console.log(userData);
			const id = Accounts.createUser({
				email: userData.email,
				password: 'p@ssW0rd',
				profile: { name: userData.name },
			});

			// email verification
			Meteor.users.update(
				{ _id: id },
				{ $set: { 'emails.0.verified': true } }
			);

			Roles.addUsersToRoles(id, userData.roles);
		});
	}
}

Meteor.startup(() => {
	process.env.MONGO_URL = Meteor.settings.MONGO_URL;
	createUsers();
});
// body.html
<template name="app_body">
	{{> loginButtons}}
		{{#if isInRole 'client'}}
			<section class="app-buttons">
				<ul>
					<li class="js-products">
						<a href="/products" class="js-products">Products</a>
					</li>
					<li class="js-profile">
						<a href="/profile" class="js-profile">Profile</a>
					</li>
					<li class="js-brands">
						<a href="/brands" class="js-brands">Brands</a>
					</li>
				</ul>
			</section>

			{{> yield}}
		{{/if }}
</template>

Hi Clayton,
Can you check your database and make sure they are in there?
You need to make sure you publish a null subscription as well.

Meteor.publish(null, function () {
  if (this.userId) {
    return Meteor.roleAssignment.find({ 'user._id': this.userId });
  } else {
    this.ready()
  }
})
1 Like

Yep, I have that bit of code running as well. And they are in my DB, the roles and role-assignments.

The way I’ve had it work for a while is to publish the entire user object and then check if it’s in role. It is just in the data so you check the key exists, there is no helper or wrapper for it. Just literally checking if the role == ‘client’ etc hope that helps

1 Like

I’ve never used that helper. I’ve always added my own helper. Something like:

Template.index.helpers({
 isInRole(): {
  return Roles.userIsInRole(Meteor.userId(), ['client'])
 }
})
1 Like