I have a express router and I only want authorized users to access routes of that router. I am using passport middleware. So I can just add a check for req.user
in every endpoint like:
router.get("/", async (req, res) => {
if (!req.user) {
return res.sendStatus(401).send({ message: "Unauthorized" });
}
//logic
res.sendStatus(200).send({message: "OK"})
});
I can add a check in every endpoint like this but is there any better way to do this?