function andRestrictToSelf(req, res, next) {
if (req.authenticatedUser.id == req.user.id) {
next();
} else {
next(new Error('Unauthorized'));
}
}
function andRestrictTo(role) {
return function(req, res, next) {
if (req.authenticatedUser.role == role) {
next();
} else {
next(new Error('Unauthorized'));
}
}
}
app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){
});
app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){ });
However I would like to have a route that is restricted to the user himself AND to an admin user:
app.get('/user/:id/moreStuff', loadUser, andRestrictToSelf, andRestrictTo('admin'), function(req, res){
res.send('Only viewable for admin and the user himself');
});
As far as I can tell this doesn't work. andRestrictToSelf would produce an error for the admin user and andRestrictTo('admin') would produce an error for the user himself. Does anyone have an idea for an elegant solution to allow access for the user and an admin?
Thanks!