How to wait until subscribed to Roles? (FlowRouter)

I have one address which I’d like to protect it from users who are not admin.
When typing the address in the address bar, the issue I have is that the client is not yet subscribed to the roles collection.

So I have something like :

FlowRouter.route(‘/admin’, {
name: ‘adminpanel’,
action(params, queryParams) {
const userId = Meteor.userId();
if(!userId){
FlowRouter.go(‘/’);
return;
}

if(!Roles.userIsInRole(userId, 'admin')) { // here it is returning false as subscription is not done yet.
  FlowRouter.go('/home'); /
  return;
}
BlazeLayout.render('adminlayout', { main: 'adminpanel'});

}
});

How can I ensure that the subscription is done before checking for it?

Using meteor 1.8.3.

1 Like

I would recommend using FlowRouter Extra and the waitOn hook

Can’t remember where I got this code block from but it does the job:

// Pause FlowRouter until Roles information is ready
FlowRouter.wait();
Tracker.autorun(() => {
    // if the roles subscription is ready, start routing
    // there are specific cases that this reruns, so we also check
    // that FlowRouter hasn't initalized already
    if (Roles.subscription.ready() && !FlowRouter._initialized) {
        FlowRouter.initialize();
    }
});
2 Likes

I installed FlowRouter Extra and the mehod waitOn wasn’t even called. I assume I did something wrong.

It worked perfectly. Thanks a lot and Merry Xmas.

1 Like

This code lines might come from https://medium.com/@satyavh/using-flow-router-for-authentication-ba7bb2644f42
There’s another good post about this issue here https://tzapu.com/meteor-alannings-roles-flow-router-not-checking-roles-routes/

1 Like