Meteor.methods({
'method.one': () => {
console.log(this.userId); // undefined
},
'method.two': function() {
console.log(this.userId); // current logged in user id
},
methodThree() {
console.log(this.userId); // current logged in user id
},
});
method.one looks cool but there is some limitations
import { PermissionsMixin } from 'meteor/didericis:permissions-mixin';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import pify from 'pify';
export const SecuredMethod = class SecuredMethod extends ValidatedMethod {
constructor(methodDefinition) {
super({
...methodDefinition,
mixins: [...methodDefinition.mixins, PermissionsMixin],
});
}
/**
*
* @param {...any} args - the arguments passed to the method
* @returns {undefined|Promise} - If a callback was not supplied, this returns a Promise
*/
call(...args) {
const maybeCb = args[args.length -1];
if (typeof maybeCb === 'function') {
return super.call(...args);
}
return this.callPromise(...args);
}
callPromise = pify(super.call.bind(this));
};
It merges the method’s definition with PermissionsMixin and then passes it on.
It also automates promisifying calls if no callback was supplied.
All the heavy lifting is done by reusable permissions objects designed for use with PermissionsMixin.
Like the builtin PermissionsMixin.LoggedIn, we have others for other groups and such
Again, keeping each concern separated out for loose coupling, re-usability and testability.
Our architecture took a lot of inspiration from meteor-tuts