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:
parent
35bf29d27d
commit
39fea74365
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;
|
Loading…
Reference in New Issue
Block a user