Meteor JS(铁路由器) - 限制对服务器路由的访问

时间:2017-10-09 13:44:02

标签: javascript meteor iron-router

我的MeteorJs应用程序中有一个下载路径,我想限制访问。路线代码如下

Router.route("/download-data",  function() {
var data = Meteor.users.find({ "profile.user_type": "employee" }).fetch();
var fields = [...fields];

var title = "Employee - Users";

var file = Excel.export(title, fields, data);

var headers = {
  "Content-type": "application/vnd.openxmlformats",
  "Content-Disposition": "attachment; filename=" + title + ".xlsx"
};

 this.response.writeHead(200, headers);
 this.response.end(file, "binary");
 },
 { where: "server" }
);

该路线自动下载文件。这当前有效,但我想限制对路线的访问。我只希望管理员能够下载它。

我创建了一个onBeforeAction Hook,如下所示

Router.onBeforeAction(
  function() {
    //using alanning:roles
    if(Roles.userIsInRole(this.userId, "admin"){
     console.log('message') //testing
   }
  },
  {
    only: ["downloadData"]
  }
);

并将我的路线重命名为

//code above
this.response.writeHead(200, headers);
 this.response.end(file, "binary");
 },
 { where: "server", name: "downloadData" }
);

onBeforeAcion挂钩不会产生任何影响

我也注意到this.userIdMeteor.userId都不适用于路线

1 个答案:

答案 0 :(得分:1)

对于服务器端钩子,我很确定你需要onBeforeAction来为你的路线设置{where:“server”}部分。

另外,我不认为铁:路由器曾经在其路由上实现了服务器端用户身份验证。您可能需要检查具有较大功能的服务器路由的程序包,例如mhagmajer:可以访问经过身份验证的路由的服务器路由器。

https://github.com/mhagmajer/server-router

相关问题