This commit is contained in:
nadja
2026-11-01 20:14:25 +01:00
parent 828c38c779
commit c4685c2152
1686 changed files with 570761 additions and 560 deletions

27
backend/dist/middleware.js vendored Normal file
View File

@@ -0,0 +1,27 @@
import jwt from 'jsonwebtoken';
import { getUserByUsername } from './db.js';
const JWT_SECRET = process.env.JWT_SECRET || 'tiktok-rezepte-secret-key-change-in-production';
export function authMiddleware(req, res, next) {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({ error: 'Nicht authentifiziert' });
}
try {
const token = authHeader.split(' ')[1];
const decoded = jwt.verify(token, JWT_SECRET);
const user = getUserByUsername(decoded.username);
if (!user)
return res.status(401).json({ error: 'Benutzer nicht gefunden' });
req.user = user;
next();
}
catch {
res.status(401).json({ error: 'Ungültiges Token' });
}
}
export function adminMiddleware(req, res, next) {
if (!req.user || !req.user.is_admin) {
return res.status(403).json({ error: 'Keine Berechtigung' });
}
next();
}