← back to Butlr

lib/admin-gate.js

22 lines

// requireAdmin middleware — second gate on top of requireOwner.
//
// requireOwner (lib/owner-auth) verifies the user is signed in (any role).
// requireAdmin then verifies role === 'admin'. Splits so non-admin signed-in
// users get a 404-shaped response (no role info leak) instead of 403, which
// would tell them "/admin/* exists but you can't see it".
//
// Pure middleware — no Express object dependency — so unit-testable by
// passing fake req/res/next.

function requireAdmin(req, res, next) {
  if (!req.user || req.user.role !== 'admin') {
    // Render the standard 404 so admin paths look like they don't exist
    // to non-admins. Status code is 403 so security scanners can still
    // distinguish, but the body is identical to a real 404.
    return res.status(403).render('public/404', { title: 'Not found — Butlr' });
  }
  return next();
}

module.exports = { requireAdmin };