23 lines
609 B
JavaScript
23 lines
609 B
JavaScript
import jwt from "jsonwebtoken";
|
|||
|
|
import { config } from "../config/index.js";
|
||
|
|
|
||
|
|
export function authenticate(req, res, next) {
|
||
|
|
const header = req.headers.authorization;
|
||
|
|
if (!header?.startsWith("Bearer ")) {
|
||
|
|
return res.status(401).json({ error: "Niet ingelogd." });
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
req.user = jwt.verify(header.slice(7), config.JWT_SECRET);
|
||
|
|
next();
|
||
|
|
} catch {
|
||
|
|
res.status(401).json({ error: "Sessie verlopen. Log opnieuw in." });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function requireAdmin(req, res, next) {
|
||
|
|
if (req.user?.rol !== "Admin") {
|
||
|
|
return res.status(403).json({ error: "Geen toegang." });
|
||
|
|
}
|
||
|
|
next();
|
||
|
|
}
|