Im using PassportJS in ExpressJS application:
In app.js, I have route for admin-only and requiresAdmin function:
var requiresAdmin = function() {
return [
ensureLoggedIn('/login'),
function(req, res, next) {
if (req.user && req.user.admin === true){
next();
}else{
res.send(401, 'Unauthorized');
}
}
];
};
app.all('/admin/*', requiresAdmin());
I created another route that will return JSON object. Also can be accessed by admin.
app.all('/api/admin/*', requiresAdmin());
If users are not logged in or non-admin, how to return JSON using the above approach? Thanks