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 166 forks source link

Roles.getScopesForUser(userId) always return [] on client #296

Closed danyhiol closed 4 years ago

danyhiol commented 4 years ago

everything works just fine on the server. But on the client, i always get: [] for Roles.getScopesForUser(userId); Actually nothing works on the client. I can't get roles or scope from the client. I've published the role as mention in the doc for version 3. in the client I have the following:

const loading = !Roles.subscription.ready();
roles: Roles.getRolesForUser(userId)

Wish always returns []. The same goes for google chrome console

alanning:roles@3.1.0
METEOR@1.8.2
SimonSimCity commented 4 years ago

Can you create a small test project where you can demonstrate the issue to me?

And ... Roles.subscription.ready() is for publishing the roles, but have you also published the role-assignments? Does Meteor.roleAssignment.find().fetch() return anything on the server and the client?

danyhiol commented 4 years ago

The server publication is just like in the doc:

Meteor.publish(null, function () {
  if (this.userId) {
    return Meteor.roleAssignment.find({ 'user._id': this.userId });
  } else {
    this.ready()
  }
});

It was mention in the doc so since roles are now stored in a separate DB, I taught it may be necessary to publish it on the server again. so when using Roles.subscription.ready() it is no more necessary to use the above code? Meteor.roleAssignment.find().fetch() returns the roles[] (_id, role, user, scope, inheritedRoles) on the server, but nothing on the client([]). I don't know how to create a test project on stackblitz for example, using meteor.

SimonSimCity commented 4 years ago

I don't know stackblitz - but I was expecting you to post the link to a github repository which I can run on my machine, debug it and change the code.

The subscription I put into the sample, which you used, only publishes the role-assignments for the user currently logged in. If you're not logged in or want to receive the permissions for another user, you'd have to generalize this publication. Have you had a look at the samples and how I had to change them to make them working with v3 of this plugin?

danyhiol commented 4 years ago

Yes, that is my aim, to publish only the actual user role-assignments. I actually from meteor shell Package['alanning:roles'].Roles._forwardMigrate2() from version 2 that was working just fine. After the migration, roles can't be accessed from the client.

SimonSimCity commented 4 years ago

Since the documents are available on the server but not on the client, it's a problem with the publication. Please analyze the websocket transfer or create a repository which shows the problem you're facing.

danyhiol commented 4 years ago

Just to complete, I'm using a GraphQL server with Apollo Client. So I guess since roles are no more published automatically, I'll have to find a way to do this myself. From my readings, I've found out that GraphQL uses WebSocket replaces Meteor-DDP (Correct me if I'm wrong). I've tried normal subscription with other DB and this was returning also an empty array. So my guess is that the problem comes from it.

danyhiol commented 4 years ago

Solved: I forgot to import my publication file on the server...