Right way to return result from Publish only for logged-in users and prevent application freeze in Flowrouter waitOn method

Right way to return result from Publish only for logged-in users and prevent application freeze in Flowrouter waitOn method?

I created method checkPermissions() which make an Meteor.Error, in case of user is not logged in for prevent return data for logged-out users. But If I make Meteor.logout() on client, application will freeze with white screen (don’t redirect to login page), because It is still waiting for response from publish. How to solve it with right way?

Route example:

adminRoutes.route('/schemes', {
    name: 'admin.schemes',
    action() {
        this.render('layout', 'adminSchemes', {
            contentTitle: 'adminSchemesTitle'
        });
    },
    waitOn(params) {
        return [
            import('./views/admin/schemes/adminSchemes_script'),
            Meteor.subscribe('settings_public')
        ];
    }
});

Publish example:

Meteor.publish("adminUserFullnames", function () {
    checkPermissions();
    return Meteor.users.find({}, {fields: {'profile.fullname': 1}});
});

Check permissions method:

export const checkPermissions = () => {
  if (!currentUserIsInRole('admin')) {
    throw new Meteor.Error("Access denied!");
  }
}

You can use Meteor.publish(null, function(…){}); to publish something when user connected to server without calling subscribe

1 Like

@afrokick Hi Alex, thanks a lot for your answer. I tried almost everything, I found, that return empty array also works. But is it right approach, how we are checking rights inside publish?

Thank you very much.