meteor-roles icon indicating copy to clipboard operation
meteor-roles copied to clipboard

Problem with Meteor.roleAssignment and typescript

Open FoudreNoire26i opened this issue 3 years ago • 4 comments

Hi, I'm trying to use meteor-roles package, but i faced a problem : Roles.userIsInRole(myId, 'admin')) always return false in client side. I read that I had to publish Meteor.roleAssignement, but I can't because I use typescript and roleAssignment is undefined. Can someone help me ? Capture d’écran 2021-06-29 à 17 28 25

Thanks

FoudreNoire26i avatar Jun 29 '21 15:06 FoudreNoire26i

Hi, I finally find a hack by myself. I keep this issue open, if anyone has a better idea (or if the maintainer want to close it ;)), but this is the best solution I found today. Meteor.publish(null, function (){ var myId = Meteor.userId() if (myId && Roles.userIsInRole(myId, 'admin')) { return [Meteor.roles.find(), (Meteor as any).roleAssignment.find()] } else { return this.ready() } });

FoudreNoire26i avatar Jun 30 '21 08:06 FoudreNoire26i

We will have to figure out how to update the Typescript typings for this. For right now you should be able to supress the warning with // @ts-ignore

StorytellerCZ avatar Apr 25 '22 19:04 StorytellerCZ

Here is the types package, but we'll need to udpate it: https://www.npmjs.com/package/@types/meteor-roles

StorytellerCZ avatar Apr 25 '22 20:04 StorytellerCZ

You can just add it to your meteor namespace: const roleAssignment: Mongo.Collection<Roles.Role>;. This is if the type package has not yet been updated.

danyhiol avatar Jul 04 '22 09:07 danyhiol

@FoudreNoire26i @danyhiol please check the latest version. Types have been added recently.

StorytellerCZ avatar Sep 23 '23 18:09 StorytellerCZ

I have the latest version installed and I can see the types being declared if I look at the definitions file in the installed package source, but for some reason I am still getting the same error. Are they perhaps being declared incorrectly? Or am I doing something wrong?

bruceborrett avatar Dec 14 '23 14:12 bruceborrett

This is the current declaration in the types for the problematic variables:

declare namespace Meteor {
  var roles: Mongo.Collection<Roles.Role>
  var roleAssignment: Mongo.Collection<Roles.RoleAssignment>
}

StorytellerCZ avatar Dec 18 '23 15:12 StorytellerCZ

If I do:

import { Meteor } from 'meteor/alanning:roles;

Then I get the correct types, but I lose all of the other Meteor types. Perhaps this is an issue with the way zodern:types handles these d.ts files because it seems the Meteor namespace is not being extended correctly.

bruceborrett avatar Feb 02 '24 16:02 bruceborrett

Looks like this is the correct definition:

declare module 'meteor/meteor' {
  namespace Meteor {
    const roles: Mongo.Collection<Roles.Role>
    const roleAssignment: Mongo.Collection<Roles.RoleAssignment>
  }
}

With the above everything works as expected.

bruceborrett avatar Feb 03 '24 06:02 bruceborrett