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
920 stars 164 forks source link

How to ensure the default publication of role-assignments is ready? #309

Closed PhilMataz closed 4 years ago

PhilMataz commented 4 years ago

Before entering the "/" route in my app, I await if Meteor.user() is defined, if so, I will redirect to "/dashboard" if not I will redirect to "/login".

Before entering each route, it is checked if the route has a permission in its meta object, if so, I check with Roles.userIsInRole wether or not the user has access, redirecting to "/" if not and to the disred route if so.

Since upgrading to V3, the Router sometimes falls into an infinite loop, I believe this happens, because the subscription for the roleAssignment is not ready yet, causing the user to endlessly loop between the "/" and the "/dashboard" route as the user is logged in but userIsInRole never passes.

Is there a way for me to make sure that the roleAssignment publication is ready? I tried naming the publication and actually waiting for it to be ready before rendering the whole app, without success. Thank you.

SimonSimCity commented 4 years ago

Well, if you set up the publication as I showed, it does not have a name - means there's no handle to it on the client side. I neither know if the system actually keeps a handle somewhere, but this you'd have to find out yourself.

What you could do on the other side is to write some code that's reactive on the user, create a new named subscription to publish the roles of the current user. In this case, you'll have a handler as on every other publication.