我不想使用额外的中间件,例如 connect-roles
,那么这足以执行授权吗?
function authWithRole(role) {
return (req, res, next) => {
// Check login and role.
if (req.isAuthenticated() &&
_.indexOf(req.user.roles, role) > -1) {
return next();
} else {
res.status(404).send('<h1>404 Not Found!</h1>');
}
}
}
router.all('/*', authWithRole("admin"));
有人可以提供一个更简单、更实用的授权示例吗?
最佳答案
是的。您可以通过这种方式进行授权。
这非常简单,无需使用任何额外的模块。
你应该重构:
router.all('/*', authWithRole("admin"));
至:
router.all('*', authWithRole("admin"));
关于javascript - 这样可以用角色验证用户吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35045974/