mirror of
https://github.com/franjsco/tick3t-api
synced 2025-06-05 22:09:20 +02:00
add helper: authentication
This commit is contained in:
28
src/helpers/authentication.js
Normal file
28
src/helpers/authentication.js
Normal 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;
|
Reference in New Issue
Block a user