-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
30 lines (29 loc) · 814 Bytes
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const jwt = require('jsonwebtoken');
module.exports = {
validateToken: (req, res, next) => {
const authorizationHeaader = req.headers.authorization;
let result;
if (authorizationHeaader) {
const token = req.headers.authorization.split(' ')[1]; // Bearer <token>
const options = {
expiresIn: '2d',
issuer: 'https://github.com/snoopysecurity',
algorithms: ["HS256", "none"],
ignoreExpiration: true
};
try {
result = jwt.verify(token, process.env.JWT_SECRET, options);
req.decoded = result;
next();
} catch (err) {
throw new Error(err);
}
} else {
result = {
error: `Authentication error. Token required.`,
status: 401
};
res.status(401).send(result);
}
}
};