仅当用户登录时才允许运行Meteor服务器方法

仅当用户登录时才允许运行Meteor服务器方法,meteor,authorization,Meteor,Authorization,我在Meteor上玩了一段时间,现在使用Meteor.methods({…}) 现在我正在检查每个方法的开头,如果用户已登录。是否有可能编写更面向方面的代码,并在另一个更集中的地方进行检查?类似于使用检查Meteor.Collection的方式。是否允许?我想让用户登录大多数方法,如果不是全部的话 服务器上是否有一些执行管道,我可以在其中插入函数来执行此操作?我想这样做已经有一段时间了 这就是我到目前为止所做的: // function to wrap methods with the prov

我在Meteor上玩了一段时间,现在使用
Meteor.methods({…})

现在我正在检查每个方法的开头,如果用户已登录。是否有可能编写更面向方面的代码,并在另一个更集中的地方进行检查?类似于使用
检查Meteor.Collection
的方式。是否允许?我想让用户登录大多数方法,如果不是全部的话


服务器上是否有一些执行管道,我可以在其中插入函数来执行此操作?

我想这样做已经有一段时间了

这就是我到目前为止所做的:

// function to wrap methods with the provided preHooks / postHooks. Should correctly cascade the right value for `this`
function wrapMethods(preHooks, postHooks, methods){
  var wrappedMethods = {};
  // use `map` to loop, so that each iteration has a different context
  _.map(methods, function(method, methodName){
    wrappedMethods[methodName] = makeFunction(method.length, function(){
      var  _i, _I, returnValue;
      for (_i = 0, _I = preHooks.length; _i < _I; _i++){
        preHooks.apply(this, arguments);
      }
      returnValue = method.apply(this, arguments);
      for (_i = 0, _I = postHooks.length; _i < _I; _i++){
        postHooks.apply(this, arguments);
      }
      return returnValue;
    });
  });
  return wrappedMethods;
}

// Meteor looks at each methods `.length` property (the number of arguments), no decent way to cheat it... so just generate a function with the required length
function makeFunction(length, fn){
  switch(length){
    case 0: 
      return function(){ return fn.apply(this, arguments); };
    case 1:
      return function(a){ return fn.apply(this, arguments); };
    case 2:
      return function(a, b){ return fn.apply(this, arguments); };
    case 3:
      return function(a, b, c){ return fn.apply(this, arguments); };
    case 4:
      return function(a, b, c, d){ return fn.apply(this, arguments); };
    // repeat this structure until you produce functions with the required length.
    default:
      throw new Error("Failed to make function with desired length: " + length)
  }
}
function checkUser(){
  check(this.userId, String);
}
Meteor.methods(wrapMethods([checkUser], [], {
  "privilegedMethod": function(a, b, c){
    return true;
  }
}));