node.js - Passport.js - 是否可以将参数传递到路由器验证功能中?

标签 node.js express authorization passport.js

我想将特定权限传递给 Passport.js 中路由上经过身份验证的调用。

这就是我现在拥有的:

app.get('/mypage', app.authenticated, function (req, res, next) {
    if (!req.user.hasPermission('myPermission')) {
        return res.redirect('/unauthorized');
    }

    // do stuff
};

var middleware = function(app) {
    app.authenticated = function (req, res, next) {
        if (req.isAuthenticated()) {
            return next();
        }

        if (req.method == 'GET') {
            req.session.returnTo = req.originalUrl;
        }

        res.redirect('/login');
    };
}

module.exports = middleware;

我想将权限传递给authenticated,如下所示:

app.get('/mypage', app.authenticated('myPermission'), function (req, res, next) {
    // do stuff
};

但据我所知,由于身份验证会自动获取所需的参数,因此我不能只添加一个新参数。

我怎样才能做到这一点?

最佳答案

您可以在任何 Express 中间件中访问 req.body 值。

在您的 app.authenticated(..) 中间件中,在执行之前设置值: req.body['permission'] = 'myPermission'

使用req.body['permission']的值进行授权。

关于node.js - Passport.js - 是否可以将参数传递到路由器验证功能中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29460451/

相关文章:

ajax - 如何使用 Node 将 HTML POST 与 Mongodb 连接

javascript - 无法从客户端获取 JSON 数据

java - 在 GWT 应用程序中处理授权

Angular - 错误引用错误 : config is not defined

node.js - 长级别升级查询

node.js - 使用 GraphQL NestJS 上传

javascript - ForbiddenError : invalid csrf token, express js

node.js - Node Express 服务器中的 CORS 正则表达式

ios - "The app must ask for a basic read permission at install time"

node.js - 找不到模块 'shelljs'