Initial commit

This commit is contained in:
2025-12-07 14:32:46 +00:00
commit 0a0969b8af
4726 changed files with 536089 additions and 0 deletions

41
middleware/verifyToken.js Executable file
View File

@@ -0,0 +1,41 @@
const jwt = require('jsonwebtoken');
if (!process.env.JWT_SECRET) {
throw new Error('JWT_SECRET não definido no .env');
}
function verifyToken(req, res, next) {
const authHeader =
req.headers['authorization'] || req.headers['Authorization'];
if (!authHeader) {
return res.status(403).json({ error: 'Token não fornecido' });
}
const match = authHeader.match(/^Bearer\s+(.+)$/i);
if (!match) {
return res
.status(403)
.json({ error: 'Token malformado. Use "Bearer <token>"' });
}
const token = match[1];
jwt.verify(token, process.env.JWT_SECRET, (err, payload) => {
if (err) {
if (err.name === 'TokenExpiredError') {
return res.status(403).json({ error: 'Sessão expirada' });
}
return res.status(403).json({ error: 'Token inválido' });
}
if (!payload?.id) {
return res.status(403).json({ error: 'Token inválido' });
}
req.user = payload;
next();
});
}
module.exports = verifyToken;