backend/middleware/authenticate.js

9 lines
274 B
JavaScript
Raw Normal View History

2023-07-28 03:36:07 +00:00
function authenticate(req, res, next) {
const token = req.header('Authorization');
if (!token) return res.sendStatus(401);
jwt.verify(token, 'SECRET_KEY', (err, decoded) => {
if (err) return res.sendStatus(401);
req.userId = decoded.userId;
next();
});
}