ldap
This commit is contained in:
22
back/src/middleware/auth.ts
Normal file
22
back/src/middleware/auth.ts
Normal 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 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user