-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
33 lines (32 loc) · 1.08 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
const jwt = require("jsonwebtoken");
const { getUser } = require("./DB/models/User");
const tokenSecret = process.env.AT_SECRET_KEY;
exports.verify = async (req, res, next) => {
const token = req.headers.authorization;
if (token) {
jwt.verify(token, tokenSecret, async (err, value) => {
if (err) {
res.status(500).json({ error: "failed to authenticate token" });
next(err);
} else if (!value.username) {
value["auth"] = false;
} else {
const res = await getUser(value.username);
if (res) {
value["auth"] = true;
if (res.admin == true) {
value["admin"] = true;
} else {
value["admin"] = false;
}
} else {
value["auth"] = false;
value["admin"] = false;
}
}
req.user = value;
req.err = err;
next();
});
} else next();
};