这样可以用角色授权用户吗?

时间:2016-01-27 19:10:09

标签: javascript node.js express passport.js

我不想使用其他中间件,例如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"));

有人能为执行授权提供更简单,更实用的示例吗?

1 个答案:

答案 0 :(得分:1)

是。您可以通过这种方式执行授权。

这很简单,没有使用任何额外的模块。

你应该重构:

router.all('/*', authWithRole("admin"));

为:

router.all('*', authWithRole("admin"));
相关问题