Publishing extra data for currently logged in user
merlinstardust opened this issue · 4 comments
When I first got started with Meteor, I remember there was a line somewhere (in the tutorial?) about how to publish extra data for the currently logged in user. This isn't in the guide, at least not directly. This could fit right under the existing Publish custom data block.
The suggested solution as I recall was to write a named publication and then subscribe to it.
Meteor.publish('userData', function () {
return Meteor.users.find(this.userId, {fields: {extraField: 1, otherField: 1});
});
But having dug into Meteor's internals, I wonder why name the publication at all. If it's restricted only for the current user, why not do what the accounts package does and publish it automatically?
Meteor.publish(null, function () {
return Meteor.users.find(this.userId, {fields: {extraField: 1, otherField: 1});
});
The other alternative that doesn't exist yet (I'll open a feature request if there's interest) would be to provide a way to configure the fields that the Accounts package publishes. So something like
Accounts.config({
customLoggedInUserFields: [
'roles',
'createdAt',
],
});
The null
publication is how I do it, and it's as yet undocumented! meteor/docs#62
@lorensr so you're publishing extra data for the logged in user with a null
pub? And you haven't seen any security issues?
I guess if they're doing it internally, it's fine. I just think it's odd that the null
pub isn't stated as the best practice way for logged in user data.
Nope, I return unless this.userId
, and the pub should re-run when userId
changes.
I think maybe it should be stated as best practice
This has been solved by implementing the third option from the OP. As documented here:
https://guide.meteor.com/accounts.html#prevent-unnecessary-data-retrival
using the defaultFieldSelector
in Accounts.config
.