-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (46 loc) · 1.29 KB
/
index.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
50
51
52
53
54
55
const jwt = require("jsonwebtoken");
require("dotenv").config();
exports.isAuth = (req, res, next) => {
let decodedToken;
const authHeader = req.get("Authorization");
const {
AUTHILIZE_JWT_SECRET_KEY: secretKey,
AUTHILIZE_STATUS_CODE: statusCode = 401,
AUTHILIZE_ERR_MESSAGE: err = "Unauthorized Access",
} = process.env;
if (!secretKey) {
return res
.status(500)
.json({ message: "jwt secret key for authilize is required" });
}
if (!authHeader) {
return res.status(+statusCode).json({ message: err });
}
const token = authHeader.split(" ")[1];
try {
decodedToken = jwt.verify(token, secretKey);
} catch (error) {
return res.status(+statusCode).json({ message: err });
}
if (!decodedToken) {
return res.status(+statusCode).json({ message: err });
}
req.decodedData = decodedToken;
next();
};
exports.isAuthPass = (req, res, next) => {
let decodedToken;
const authHeader = req.get("Authorization");
const { AUTHILIZE_JWT_SECRET_KEY: secretKey } = process.env;
let token;
if (authHeader) token = authHeader.split(" ")[1];
if (token && token !== "null") {
try {
decodedToken = jwt.verify(token, secretKey);
} catch (error) {
next();
}
}
if (decodedToken) req.decodedData = decodedToken;
next();
};