File size: 475 Bytes
81fc505 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import jwt from "jsonwebtoken";
import { env } from "../config/env.js";
export function requireAuth(req, res, next) {
const header = req.headers.authorization || "";
const token = header.startsWith("Bearer ") ? header.slice(7) : null;
if (!token) {
return res.status(401).json({ error: "Missing token" });
}
try {
req.user = jwt.verify(token, env.jwtSecret);
return next();
} catch {
return res.status(401).json({ error: "Invalid token" });
}
}
|