Created
January 24, 2020 15:46
-
-
Save AshutoshSajan/6bfcbdd2863db7c6e5801783635a9d41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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