1
0
mirror of https://github.com/franjsco/tick3t-api synced 2024-12-22 13:05:47 +01:00

add helper: authentication

This commit is contained in:
Francesco Esposito 2019-08-06 18:49:02 +02:00
parent 35bf29d27d
commit 39fea74365

View File

@ -0,0 +1,28 @@
import jwt from 'jsonwebtoken';
const validateUser = (req, res, next) => {
let token = req.headers['x-access-token'] || req.headers.authorization;
if (token) {
token = token.slice(7, token.length);
jwt.verify(token, req.app.get('secretKey'), (err, decoded) => {
if (err) {
res.json({
success: false,
message: 'Token is not valid',
});
} else {
req.decoded = decoded;
next();
}
});
} else {
return res.status(401).json({
success: false,
message: 'Auth token is not supplied',
});
}
};
export default validateUser;