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