-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddlewares.js
51 lines (45 loc) · 1.18 KB
/
middlewares.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
const jwt = require('jsonwebtoken')
//:1 check token for admin
check_token = function (req, res, next) {
if (!req.headers.authorization) {
res.status(401)
res.json({ status: 401, url: req.url })
return
}
const token = req.headers.authorization.split(' ')[1]
jwt.verify(token, 'secret', function (err, decoded) {
if (err || decoded.type !== 'admin') {
res.status(401)
res.json({ status: 401, url: req.url })
return
}
})
next()
}
//:1 check token for student
student_token = function (req, res, next) {
if (!req.headers.authorization) {
res.status(401);
res.json({ status: 401, url: req.url });
return;
}
next();
}
//:1 check token for admision staff
authenticatestaff = function (req, res, next) {
const token = req.headers.authorization.split(' ')[1]
if (!token) {
return res.status(401).send("Access denied. No token provided.");
}
try {
const decoded = jwt.verify(token, 'secret');
req.user = decoded;
next();
} catch (error) {
res.status(400).send("Invalid token.");
}
}
// endfold
exports.check_token = check_token
exports.studen_token = student_token
exports.authenticatestaff = authenticatestaff