This commit is contained in:
Patryk Koreń
2026-07-02 23:55:40 +02:00
parent 04e34d15b5
commit e7ec832f5b
53 changed files with 9985 additions and 17 deletions

View File

@@ -0,0 +1,22 @@
import jwt from "jsonwebtoken";
import type { Request, Response, NextFunction } from "express";
export function authMiddleware(req: Request, res: Response, next: NextFunction) {
const header = req.headers.authorization;
const error = "You must be logged in to access this page"
if (!header) {
return res.status(401).json({ error });
}
const token = header.split(" ")[1]; // "Bearer TOKEN"
try {
if (!token) throw new Error("No Bearer Token")
const decoded = jwt.verify(token, process.env.JWT_SECRET!);
(req as any).user = decoded;
next();
} catch (err) {
return res.status(401).json({ error });
}
}