I’m using the accounts package, which apparently publishes all users to the client automatically. I’m building a system where users are organised into teams, so I’d like to publish only users from the same team, not all of them. Despite all my efforts, all users from all teams are still accessible on the client side, is there a way to prevent that?
This is how I’d like to publish users:
if (Meteor.isServer) {
Meteor.publish("team_users", function (team_id) {
return Meteor.users.find({team_id:team_id});
}
}
Thanks Steve. I’m not using the autopublish package. Doesn’t the accounts package automatically publish all users (even if only the profile object is exposed)? Trying to figure out if I’m publishing it elsewhere, though I’ve looked everywhere.
AFAIK, users are not published automatically. See here:
Like all Mongo.Collections, you can access all documents on the server, but only those specifically published by the server are available on the client.
By default, the current user’s username, emails and profile are published to the client. You can publish additional fields for the current user with: […]
If the autopublish package is installed, information about all users on the system is published to all clients.
Why do you believe all users have been published, how do you check?
Here’s how I check: while debugging I use Mongol, which allows me to visualise the documents that are available on the client side. In production I just run Meteor.users.find().fetch() on the browser’s console.
Thanks garrilla. That publishes the extra fields for users that meet the query, but it still publishes the profile object for all other users, even if the user is not logged in.
It doesn’t. I find it very strange, but after searching for many hours I’m starting to believe there’s no way of publishing some users while keeping other unpublished. The closest I found is what @garrilla said, which allows you to publish some fields for some users while keeping only the profile object visible for the rest. But that still exposes the total number of users and the profile fields.
SOLVED: you’re both right @Steve and @garrilla. There was another publication in a different place. I started building this on top of an existing app and thought I had cleaned it all up. My bad, thanks guys!