我想将特定权限传递给 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/