Feathersjs 管理员角色(或带有身份验证检查的羽毛中间件)
Feathersjs administrator role (or feathers middleware with auth check)
我有一个关于 feathersjs auth hooks(或其他)的愚蠢问题。这是我的注释代码:
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'
})
});
我找不到在哪里可以实现 user-auth 挂钩来检查管理员角色的用户。我该怎么做?
您应该可以使用我在另一个问题中发布的示例:
对于您的情况,您需要执行一些逻辑来查看用户是否是管理员。默认情况下,Feathers 为您提供的 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
属性 添加到智威汤逊负载。一旦预发布版本发布,这将变得相当简单。在那之前,以上可能是最好的方法。
我有一个关于 feathersjs auth hooks(或其他)的愚蠢问题。这是我的注释代码:
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'
})
});
我找不到在哪里可以实现 user-auth 挂钩来检查管理员角色的用户。我该怎么做?
您应该可以使用我在另一个问题中发布的示例:
对于您的情况,您需要执行一些逻辑来查看用户是否是管理员。默认情况下,Feathers 为您提供的 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
属性 添加到智威汤逊负载。一旦预发布版本发布,这将变得相当简单。在那之前,以上可能是最好的方法。