2017-03-02 46 views
1

我有一个愚蠢的问题与feathersjs auth钩子(或其他)。这是我的代码与评论:Feathersjs管理员角色(或与auth检查羽毛中间件)

app.get('/admin', function (req, res) { 

    // i'd like to check here if (req.connection.isAdmin) then render page 

    res.render('admin/admin.html', { 
     title: 'admin' 
    }) 
    }); 

我找不到在哪里我可以实现用户身份验证钩为chek用户管理角色。我怎样才能做到这一点?

+0

一些谷歌搜索后,我发现这个相同的问题https://github.com/feathersjs/feathers/issues/357但没有解决方法和页面上的链接被打破。仍然误解如何在中间件上实现钩子,或者只是使用自定义认证逻辑....如果我可以访问req,请使用连接凭证res – Victor

回答

2

您应该能够使用我张贴在这个其他问题例如:Feathers Js Restrict Access To Page on Server Side

在你的情况,你需要做一些逻辑来看看用户是管理员。默认情况下,羽毛为您提供的JWT令牌将仅包含userId。您可以检索用户记录以检查用户是否是管理员。

这里是你会怎样把jwt.verify块中的例子:

jwt.verify(token, secret, function(err, decoded) { 
    if (err) { 
    return res.status(401).send('You are not authorized to view that page.'); 
    } 
    app.service('users') 
    .get(decoded.id) // Or _id if you're using MongoDB 
    .then(user => { 
     if (user.isAdmin) { 
     res.render('admin/admin.html', { 
      title: 'admin' 
     }) 
     } else { 
     return res.status(401).send('You are not authorized to view that page.'); 
     } 
    }) 
}); 

它将在feathers-authentication下一个版本值添加到JWT在服务器上是可能的,所以,对于管理员,您可以将isAdmin属性添加到JWT有效负载。一旦预发布版本发布,这将是相当微不足道的。在此之前,上述可能是最好的选择。

+1

非常感谢Marshall!我接受你的答案。 – Victor