-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
49 lines (43 loc) · 1.19 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
let jwt = require('jsonwebtoken');
const config = require('./config/keys');
let checkToken = (req, res, next) => {
let token = req.headers['x-access-token'] || req.headers['authorization']; // Express headers are auto converted to lowercase
if (token) {
if (token.startsWith('Bearer ')) {
// Remove Bearer from string
token = token.slice(7, token.length);
}
}
if (token) {
jwt.verify(token, config.secret, (err, decoded) => {
if (err) {
console.log(err);
return res.json({
success: false,
message: 'Token is not valid'
});
} else {
req.decoded = decoded;
if(req.decoded.name!="[email protected]"){
console.log(req.decoded);
next();
} else{
console.log(req.decoded);
return res.json({
success: false,
message: 'You are Admin ... Go out!'
});
}
// next();
}
});
} else {
return res.json({
success: false,
message: 'Auth token is not supplied'
});
}
};
module.exports = {
checkToken: checkToken
}