meteor-roles: Roles.userIsInRole returns false in onBeforeAction

Updated to latest iron:router and latest alanning:roles and Meteor 0.9.3, and now Roles.userIsInRole does not seem to work within a route’s onBeforeAction.

this.route('admin', {
    onBeforeAction: function (pause) {
      if (!Roles.userIsInRole(Meteor.userId(), 'admin')) {
        console.log(Roles.userIsInRole(Meteor.userId(), 'admin'), Meteor.userId());
        this.redirect("home");
      }
    }
  });

I can see from the console.log that the user ID is correct, but userIsInRole is returning false. However, after it redirects, I can enter Roles.userIsInRole(Meteor.userId(), 'admin') in the browser console and it prints true.

So it seems like the roles are not being correctly determined until after onBeforeAction runs. Possibly I need to waitOn the subscription to the roles field, but I don’t think I had to do that before.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 18 (7 by maintainers)

Most upvoted comments

@leebenson, it still won’t be correct unless this subscription is ready. So that publication should be named and the client sub handle should be exported. Yes, we could publish the roles field in a separate pub/sub, but that seems inefficient if the roles pkg is already publishing it.

Actually Meteor.user() is undefined, too