blancopado
blancopado

Reputation: 13

Meteor-roles (alanning). Roles.userIsInRole return false

I am using the meteor-role package and I have a problem when redirecting on Flowrouter. I have a button in the navbar that redirects the user to home, which is different depending on role. The problem is that the role package take some time to be ready and the error is thrown. Any ideas on how I could solve this problem? Thanks in advance.

FlowRouter.route( '/', {
  name: 'home',
  triggersEnter: [() => {
    if (Meteor.userId()) {
      if (Roles.userIsInRole(Meteor.userId(), 'student')) {
        FlowRouter.go('internships_next');
      } else if (Roles.userIsInRole(Meteor.userId(), 'organization')) {
        FlowRouter.go('user_internships');
      } else {
        throw new Meteor.Error( 500,
          'There was an error processing your request. User id: ' + Meteor.userId()
        );
      }
    }
  }],
  action() {
    mount(LayoutContainer, {
      content: <LoginContainer/>,
    });
  },
});

Upvotes: 0

Views: 378

Answers (1)

Pankaj Jatav
Pankaj Jatav

Reputation: 2184

You need to make sure that Roles.subscription.ready() is true before redirect:

FlowRouter.route( '/', {
  name: 'home',
  action() {
    Tracker.autorun(function() {
      if (Meteor.userId() && Roles.subscription.ready()) {
        if (Roles.userIsInRole(Meteor.userId(), 'student')) {
          FlowRouter.go('internships_next');
        } else if (Roles.userIsInRole(Meteor.userId(), 'organization')) {
          FlowRouter.go('user_internships');
        } else {
          mount(LayoutContainer, {
             content: <LoginContainer/>,
          });
        }
      });
    }
  },
});

Upvotes: 2

Related Questions