Skip to content

Instantly share code, notes, and snippets.

@AshutoshSajan
Created January 24, 2020 15:46
Show Gist options
  • Save AshutoshSajan/6bfcbdd2863db7c6e5801783635a9d41 to your computer and use it in GitHub Desktop.
Save AshutoshSajan/6bfcbdd2863db7c6e5801783635a9d41 to your computer and use it in GitHub Desktop.
function isAdmin (req, res, next) {
const id = req.user.userId;
User.findOne({
_id: id
}, (err, user) => {
if (err) {
res.status(500).json({
success: false,
message: "server error",
error: err
});
} else if (!user) {
res.status(400).json({
success: false,
message: "user not found",
error: err
});
} else if (!user.isAdmin) {
res.status(403).json({
success: false,
message: "unauthorized",
error: err
});
} else if (user && user.isAdmin) {
req.user.isAdmin = user.isAdmin;
next();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment