auth.js 385 B

123456789101112131415
  1. import { config } from "../config/index.js";
  2. export function authMiddleware(req, res, next) {
  3. if (!config.apiKey) return next();
  4. const header = req.header("authorization") ?? "";
  5. const token = header.startsWith("Bearer ") ? header.slice("Bearer ".length) : "";
  6. if (token !== config.apiKey) {
  7. res.status(401).json({ error: "unauthorized" });
  8. return;
  9. }
  10. next();
  11. }