Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 9x 2x 2x 7x 2x 2x 2x 2x | // Authentication middleware
function requireAuth(req, res, next) {
if (!req.session || !req.session.user_id) {
// If API call, return 401
Eif (req.originalUrl.startsWith("/api/")) {
return res.status(401).json({ error: "Unauthorized" });
}
// Otherwise redirect to login
return res.redirect("/login");
}
next();
}
function requireAdmin(req, res, next) {
Iif (!req.session || !req.session.user_id) {
return res.status(401).json({ error: "Unauthorized" });
}
Iif (req.session.role !== "admin") {
return res.status(403).json({ error: "Forbidden - Admin access required" });
}
next();
}
module.exports = {
requireAuth,
requireAdmin,
};
|