cos tam dziala
This commit is contained in:
@@ -2,30 +2,17 @@ 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 });
|
||||
// }
|
||||
|
||||
const token = req.cookies.token;
|
||||
if (!token) return res.sendStatus(401);
|
||||
try {
|
||||
const decoded = jwt.verify(token, process.env.JWT_SECRET!) as jwt.JwtPayload;
|
||||
req.user = decoded;
|
||||
const token = req.cookies.token;
|
||||
|
||||
if (!token) { return res.sendStatus(401); }
|
||||
|
||||
console.log("Token: ", token)
|
||||
console.log("Secret: ", process.env.JWT_SECRET)
|
||||
jwt.verify(token, process.env.JWT_SECRET!)
|
||||
|
||||
next();
|
||||
|
||||
} catch {
|
||||
res.sendStatus(403);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user