-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
35 lines (31 loc) · 867 Bytes
/
middleware.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
31
32
33
34
35
let jwt = require('jsonwebtoken');
const config = require('./config.js');
let checkToken = (req, res, next) => {
let token = req.headers['x-access-token'] || req.headers['authorization']; // Express headers are auto converted to lowercase
console.log('tokennnnnnn', token);
if (token) {
if (token.startsWith('Bearer ')) {
// Remove Bearer from string
token = token.slice(7, token.length);
}
jwt.verify(token, config.secret, (err, decoded) => {
if (err) {
return res.status(401).send([{
success: false,
message: 'Token is not valid'
}]);
} else {
req.decoded = decoded;
next();
}
});
} else {
return res.json(JSON.stringify({
success: false,
message: 'Auth token is not supplied'
}));
}
};
module.exports = {
checkToken: checkToken
}