Reorganize and improve error responses

This commit is contained in:
2020-05-11 00:03:48 +02:00
parent dd6fa0e63c
commit 79a8a2fb75
8 changed files with 138 additions and 74 deletions

View File

@@ -0,0 +1,20 @@
const jwt = require('jsonwebtoken');
const { jwt_secret } = require('../config/config');
function authenticate(req, res, next) {
const token = req.get('Authorization');
if (token) {
jwt.verify(token, jwt_secret, (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',
});
}
}
exports.authenticate = authenticate;