backend/middlewares/authenticate.js

21 lines
488 B
JavaScript
Raw Normal View History

const jwt = require('jsonwebtoken');
const { jwtSecret } = require('../config/config');
function authenticate(req, res, next) {
const token = req.get('Authorization');
if (token) {
jwt.verify(token, jwtSecret, (err, decoded) => {
if (err) return res.status(401).json(err);
req.decoded = decoded;
next();
});
} else {
return res.status(401).json({
error: 'No token provided, must be set on the Authorization Header',
});
}
}
2020-05-10 22:03:48 +00:00
exports.authenticate = authenticate;