流星JS(铁路由器) - 限制访问服务器路线

问题描述:

我有一个下载路线在我的MeteorJs应用程序,我想限制访问。路由代码如下流星JS(铁路由器) - 限制访问服务器路线

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挂钩如下

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.userId也不Meteor.userId作品的路线

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

此外,我不认为铁:路由器曾经实施服务器端用户认证他们的路由。您可能需要检查服务器路由周围的包,并使用更大的功能,如mhagmajer:可以访问经过身份验证的路由的服务器路由器。

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