Run some server-side code before method called

There is a working app with many methods. And now I need to check user permissions when some methods are called. So is it possible to perform something like this when client calls method?

client:
Meteor.call('methodName',args,(err,res)=>{ ... });

server:
...
var ok=permitMe(methodName,args); // function checking permisions
if(ok){
    // permitted
    // call methodName with args and return result
}else{  
    // denied
    return new Error( ... );
}

Or should I implement checking inside every method?

For a long time, I had a function I would call at the top of each method. But, validated methods are a very nice solution: https://github.com/meteor/validated-method. Now I have a mixin for checking user permissions, a mixin for checking admin permissions, etc. and just attach the mixin to the method. Super simple!

1 Like

Nice solution, thanks. But each of the methods still needs to be modified, if I got it right.

Nah, you can use a mixin. Then you only need to write the check once and include it as a mixin for each method.