2017-10-09 191 views
0

我有一個下載路線在我的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作品的路線

回答

1

對於服務器端鉤子,我非常肯定你需要onBeforeAction來爲你的路線配置{where:「server」}部分。

此外,我不認爲鐵:路由器曾經實施服務器端用戶認證他們的路由。您可能需要檢查服務器路由周圍的包,並使用更大的功能,如mhagmajer:可以訪問經過身份驗證的路由的服務器路由器。

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