Meteor-Community-Packages / meteor-roles

Authorization package for Meteor, compatible with built-in accounts packages
http://meteor-community-packages.github.io/meteor-roles/
MIT License
921 stars 167 forks source link

Usage with FlowRouter.triggers #220

Closed Netherdrake closed 7 years ago

Netherdrake commented 7 years ago

I would like to do this:

FlowRouter.triggers.enter([
    function (context, redirect) {
        if (!Meteor.userId()) {
            FlowRouter.go('home');
        }
        if (!Roles.userIsInRole(Meteor.userId(), 'admin')) {
            console.log(Roles.userIsInRole(Meteor.userId(), 'admin'))
            FlowRouter.go('home');
        }
    }
]);

However Roles.userIsInRole(Meteor.userId(), 'admin') always returns false when called from FlowRouter.triggers. It does return true when called from browser console.

Any ideas for how could I achieve this?

jhuenges commented 7 years ago

This doesnt work because Meteor.user might not be available or the Roles collection is not available because the subscription did not finish yet.

Maybe this article can help.

Netherdrake commented 7 years ago

There is a solution on the bottom of this post that seems to work.