2020-05-08 11:12:10 +00:00
|
|
|
const jwt = require('jsonwebtoken');
|
2020-08-29 08:48:21 +00:00
|
|
|
const { jwtSecret } = require('../config/config');
|
2020-05-08 11:12:10 +00:00
|
|
|
|
|
|
|
function authenticate(req, res, next) {
|
|
|
|
const token = req.get('Authorization');
|
|
|
|
|
|
|
|
if (token) {
|
2020-08-29 08:48:21 +00:00
|
|
|
jwt.verify(token, jwtSecret, (err, decoded) => {
|
2020-05-08 11:12:10 +00:00
|
|
|
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;
|