forked from jiitopticachapter/JSCOP-6.0-BACKEND
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
36 lines (33 loc) · 1.17 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
const User = require("./models/userModel");
const jwt = require("jsonwebtoken");
const admin = require("./models/adminModel");
const Volunteer = require("./VolunteerData");
module.exports.isAdmin = async (req, res, next) => {
console.log("Admin Check!!");
const token1 = req.signedCookies.jwt;
const token2 = req.header("Authorization")?.replace("Bearer ", "");
const token = token1 || token2;
if (token) {
console.log("Token Found");
const { id } = jwt.verify(token, `${process.env.SECRET}`);
const Admin = await admin.find({ id });
if (Admin) next();
else res.status(400).json("not admin");
} else {
res.status(400).json("no token");
}
};
module.exports.isVolunteer = async (req, res, next) => {
console.log("Volunteer Check!!");
const token = req.signedCookies.jwt;
if (token) {
const decoded = jwt.verify(token, `${process.env.SECRET}`);
const volunteer = Volunteer.find(
(volunteer) => volunteer.email === decoded.email
);
if (volunteer) next();
else res.status(400).json("not volunteer");
} else {
res.status(400).json("no token");
}
};