-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
44 lines (37 loc) · 1.06 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
let jwt = require('jsonwebtoken')
const secret = '1234567890'
let checkToken = (req, res, next) => {
let token = req.headers['x-access-token'] || req.headers['authorization'] || req.headers['Access-Control-Allow-Origin: *']
if (req.headers.authorization === undefined || req.headers.authorization === null || req.headers.authorization === '') {
return res.json({
status: 403,
message: 'Unauthorized access'
});
}else{
if (token.startsWith('Bearer')) {
// Remove Bearer from string
token = token.slice(7, token.length)
}
if (token) {
jwt.verify(token, secret, (err, decoded) => {
if (err) {
return res.json({
status: 403,
message: 'Token is not valid'
});
} else {
req.decoded = decoded;
next();
}
});
} else {
return res.json({
status: 403,
message: 'Auth token is not supplied'
});
}
}//end
};
module.exports = {
checkToken: checkToken,
}