front/stubs/api/middlewares/verify.js
Nikolai Petukhov 51618c8858 small changes
2024-10-12 12:23:25 +03:00

23 lines
551 B
JavaScript

const jwt = require('jsonwebtoken');
const { TOKEN_KEY } = require('../key')
function verifyToken(req, res, next) {
const token = req.headers['authorization']?.split(' ')[1];
if (!token) {
return res.status(401).send({ message: 'No token provided' });
}
// 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;