var jwt = require('jsonwebtoken');
function auth(req, res, next) {
try {
// Assuming the token is sent in the Authorization header as "Bearer <token>"
const token = req.headers.authorization.split(' ')[1];
jwt.verify(token, 'hello', function(err, decoded) {
if (err) {
return res.status(401).json({
status: "error",
message: "Unauthorized"
});
}
req.userData = decoded;
next();
});
} catch (error) {
return res.status(500).json({
status: "error",
message: "Internal Server Error"
});
}
}
module.exports = auth;