Files
Showy/backend/middleware/authMiddleware.js
T

27 lines
738 B
JavaScript

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;