JWT_SECRET and ADMIN_KEY no longer fall back to insecure defaults. The app will throw at startup if these env vars are not set. docker-compose.yml now uses :? syntax to require them. Co-authored-by: Cursor <cursoragent@cursor.com>
27 lines
679 B
JavaScript
27 lines
679 B
JavaScript
const jwt = require('jsonwebtoken');
|
|
|
|
if (!process.env.JWT_SECRET) {
|
|
throw new Error('JWT_SECRET environment variable is required');
|
|
}
|
|
const JWT_SECRET = process.env.JWT_SECRET;
|
|
|
|
function authenticateToken(req, res, next) {
|
|
const authHeader = req.headers['authorization'];
|
|
const token = authHeader && authHeader.split(' ')[1]; // Bearer TOKEN
|
|
|
|
if (!token) {
|
|
return res.status(401).json({ error: 'Access token required' });
|
|
}
|
|
|
|
jwt.verify(token, JWT_SECRET, (err, user) => {
|
|
if (err) {
|
|
return res.status(403).json({ error: 'Invalid or expired token' });
|
|
}
|
|
req.user = user;
|
|
next();
|
|
});
|
|
}
|
|
|
|
module.exports = { authenticateToken, JWT_SECRET };
|
|
|