Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Be able to define default user fields published on login #11118

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 39 additions & 22 deletions packages/accounts-base/accounts_server.js
Expand Up @@ -37,6 +37,14 @@ export class AccountsServer extends AccountsCommon {
loggedInUser: ['profile', 'username', 'emails'],
otherUsers: ['profile', 'username']
};

// use object to keep the reference when used in functions
// where _defaultPublishFields is destructured into lexical scope
// for publish callbacks that need `this`
this._defaultPublishFields = {
loggedInUser: ['profile', 'username', 'emails'],
menelike marked this conversation as resolved.
Show resolved Hide resolved
};

this._initServerPublications();

// connectionId -> {connection, loginToken}
Expand Down Expand Up @@ -678,39 +686,40 @@ export class AccountsServer extends AccountsCommon {

_initServerPublications() {
// Bring into lexical scope for publish callbacks that need `this`
const { users, _autopublishFields } = this;
const { users, _autopublishFields, _defaultPublishFields } = this;

// Publish all login service configuration fields other than secret.
this._server.publish("meteor.loginServiceConfiguration", () => {
const { ServiceConfiguration } = Package['service-configuration'];
return ServiceConfiguration.configurations.find({}, {fields: {secret: 0}});
}, {is_auto: true}); // not techincally autopublish, but stops the warning.

// Publish the current user's record to the client.
this._server.publish(null, function () {
if (this.userId) {
return users.find({
_id: this.userId
}, {
fields: {
profile: 1,
username: 1,
emails: 1
}
});
} else {
return null;
}
}, /*suppress autopublish warning*/{is_auto: true});
// ['profile', 'username'] -> {profile: 1, username: 1}
const toFieldSelector = fields => fields.reduce((prev, field) => (
menelike marked this conversation as resolved.
Show resolved Hide resolved
{ ...prev, [field]: 1 }),
{}
);

// Use Meteor.startup to give other packages a chance to call
// addDefaultPublishFields.
Meteor.startup(() => {
// Publish the current user's record to the client.
this._server.publish(null, function () {
if (this.userId) {
return users.find({
_id: this.userId
}, {
fields: toFieldSelector(_defaultPublishFields.loggedInUser),
});
} else {
return null;
}
}, /*suppress autopublish warning*/{is_auto: true});
});

// Use Meteor.startup to give other packages a chance to call
// addAutopublishFields.
Package.autopublish && Meteor.startup(() => {
// ['profile', 'username'] -> {profile: 1, username: 1}
const toFieldSelector = fields => fields.reduce((prev, field) => (
{ ...prev, [field]: 1 }),
{}
);
this._server.publish(null, function () {
if (this.userId) {
return users.find({ _id: this.userId }, {
Expand Down Expand Up @@ -749,6 +758,14 @@ export class AccountsServer extends AccountsCommon {
this._autopublishFields.otherUsers, opts.forOtherUsers);
};

// Add to the list of fields or subfields to be automatically
// published when the user logs in
//
// @param fields {Array.<string>} fields
addDefaultPublishFields(fields) {
this._defaultPublishFields.loggedInUser = [...new Set([...this._defaultPublishFields.loggedInUser, ...fields])];
};

///
/// ACCOUNT DATA
///
Expand Down