refacto: Keeping entrypoints clean and making files by purpose
This commit is contained in:
18
server/middleware/auth.js
Normal file
18
server/middleware/auth.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import jwt from "jsonwebtoken";
|
||||
|
||||
export const JWT_SECRET = process.env.JWT_SECRET ?? "dev_secret_change_me";
|
||||
|
||||
export function authMiddleware(req, res, next) {
|
||||
const authHeader = req.headers["authorization"];
|
||||
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
||||
return res.status(401).json({ error: "unauthorized" });
|
||||
}
|
||||
const token = authHeader.slice(7);
|
||||
try {
|
||||
const payload = jwt.verify(token, JWT_SECRET);
|
||||
req.user = payload;
|
||||
next();
|
||||
} catch {
|
||||
return res.status(401).json({ error: "invalid_token" });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user