cos tam dziala

This commit is contained in:
Patryk Koreń
2026-07-06 23:17:37 +02:00
parent e7ec832f5b
commit ff707dd762
26 changed files with 494 additions and 60 deletions

View File

@@ -2,21 +2,31 @@ 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"
// const header = req.headers.authorization;
// const error = "You must be logged in to access this page"
if (!header) {
return res.status(401).json({ error });
}
// if (!header) {
// return res.status(401).json({ error });
// }
const token = header.split(" ")[1]; // "Bearer TOKEN"
// 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 });
// }
const token = req.cookies.token;
if (!token) return res.sendStatus(401);
try {
if (!token) throw new Error("No Bearer Token")
const decoded = jwt.verify(token, process.env.JWT_SECRET!);
(req as any).user = decoded;
const decoded = jwt.verify(token, process.env.JWT_SECRET!) as jwt.JwtPayload;
req.user = decoded;
next();
} catch (err) {
return res.status(401).json({ error });
} catch {
res.sendStatus(403);
}
}