front/stubs/api/middlewares/verify.js

23 lines
551 B
JavaScript
Raw Permalink Normal View History

2024-10-03 22:35:39 +03:00
const jwt = require('jsonwebtoken');
2024-10-12 12:23:25 +03:00
const { TOKEN_KEY } = require('../key')
2024-10-03 22:35:39 +03:00
function verifyToken(req, res, next) {
const token = req.headers['authorization']?.split(' ')[1];
if (!token) {
2024-10-12 12:23:25 +03:00
return res.status(401).send({ message: 'No token provided' });
2024-10-03 22:35:39 +03:00
}
// Verify token
jwt.verify(token, TOKEN_KEY, (err, decoded) => {
if (err) {
return res.status(401).send({ message: 'Unauthorized' });
}
next(); // Proceed to the next middleware or route
});
}
module.exports = verifyToken;