Raggamuffin
Raggamuffin

Reputation: 709

Flowrouter with alanning:Roles - Delay in 'userIsInRole' Method

I'm using FlowRouter with the alanning:roles package to check on every routeEnter for a certain route group if the user has the role as an admin.

let adminRoutes = FlowRouter.group({
    name: "adminRoutes",
    triggersEnter: [checkAdmin]
});

checkAdmin = () => {
console.log(Roles.userIsInRole(Meteor.userId(), 'admin')); // false
if (! Roles.userIsInRole(Meteor.userId(), 'admin')) {
    console.log(Roles.userIsInRole(Meteor.userId(), 'admin')); //false
    FlowRouter.redirect('/notFound');
}

};

When i log in as an admin the console log is first false and i get redirected to the notFound page. When i hit the back-button it seems the role is initialised and i can actually see the admin area.

How can i avoid this delay in initialising the admin role?

Thanks for your help!

Muff

Upvotes: 1

Views: 95

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20226

The user object takes a few cycles to be fully loaded so if you check it immediately you won't have access to all the user's properties. It's best to show a spinner while waiting for the user object to be loaded. You can create a subscription to the user that includes only those keys you really need and wait for that subscription to be ready.

Upvotes: 1

Related Questions