-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckToken.js
More file actions
35 lines (32 loc) · 843 Bytes
/
Copy pathCheckToken.js
File metadata and controls
35 lines (32 loc) · 843 Bytes
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
import jwt from "jsonwebtoken";
import environment from "../config/environment";
export const CheckToken = function(req, res, next) {
try {
let token = req.headers["x-access-token"] || req.headers["authorization"]
if (token.startsWith("Bearer ")) {
// Remove Bearer from string
token = token.slice(7, token.length)
}
if (!token)
return res.status(401).send({
success: false,
message: "Unauthorized! Auth token is not supplied!"
})
jwt.verify(token, environment.privateJWT, (err, decoded) => {
if (err) {
return res.status(401).send({
success: false,
message: "Unauthorized! Token is not valid!"
})
} else {
req.decoded = decoded
next()
}
})
} catch (e) {
return res.status(401).send({
success: false,
message: "Unauthorized! Auth token is not supplied!"
})
}
}