dimanche 5 avril 2015

Passport.js - Is it possible to pass a parameter into the router authenticated function?

I would like to pass a certain permission into the authenticated call on routes in Passport.js.


This is what I have now:



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;


I would instead like to pass the permission into authenticated like this:



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


But as far as I can tell, since authenticated gets the parameters it needs automatically, I can't just add a new one.


How can I go about doing this?


Aucun commentaire:

Enregistrer un commentaire