Protect backend routes with JWT auth middleware

This commit is contained in:
X4Lo committed 2026-06-23 04:14:51 +01:00
1 parent b3a4eb9fde
commit 4313a794ce
3 files changed
+44 -8

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
const jwt = require('jsonwebtoken');
const configService = require('../services/configService');
function authMiddleware(req, res, next) {
// Skip auth for login/verify endpoints
if (req.path === '/auth/login' || req.path === '/auth/verify') {
return next();
}
const auth = req.headers.authorization;
if (!auth || !auth.startsWith('Bearer ')) {
return res.status(401).json({ error: 'Authentication required' });
}
const settings = configService.getSettings();
const jwtSecret = settings.jwtSecret || 'showy-default-secret';
try {
jwt.verify(auth.slice(7), jwtSecret);
next();
} catch {
return res.status(401).json({ error: 'Invalid or expired token' });
}
}
module.exports = authMiddleware;