-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
90 lines (81 loc) · 3.36 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
You can choose to define all your middleware functions here,
export them and then import them into your app.js and attach them that that.
add.use(myMiddleWare()). you can also just define them in the app.js if you like as seen in lecture 10's lecture code example. If you choose to write them in the app.js, you do not have to use this file.
*/
const middlewareMethods = {
description: 'This is my helper function for lab-6',
checkAuthentication: (req, res, next) => {
if (!req.session.user) {
return res.redirect('/login');
} else {
if (req.session.user.role.toLowerCase() === 'primary user') {
return res.redirect('/primaryuser');
} else if (req.session.user.role.toLowerCase() === 'scout user') {
return res.redirect('/scoutuser');
}
}
next();
},
loginMiddleware: (req, res, next)=>{
if(!req.session.user){
//not authenticated so i am allowing it to reach the login route
next();
}else{
if(req.session.user.role.toLowerCase() === 'primary user'){
return res.redirect('/primaryuser')
}else if(req.session.user.role.toLowerCase() === 'scout user'){
return res.redirect('/scoutuser')
}
}
},
registerMiddleware: (req, res, next)=>{
if(!req.session.user){
//user is not authenticated so i am letting it reach the register route
next();
}else{
//doing the same logic as above
if(req.session.user.role.toLowerCase() === 'primary user'){
return res.redirect('/primaryuser')
}else if(req.session.user.role.toLowerCase() === 'scout user'){
return res.redirect('/scoutuser')
}
}
},
protectedMiddleware: (req, res, next)=>{
if(!req.session.user){
//hitting GET/login since user is not authenticated
return res.redirect('/login');
}else{
//letting it fall through if authenticated user, as mentioned in the ruberics
next();
}
},
adminMiddleware: (req, res, next)=>{
if(!req.session.user){
return res.redirect('/login');
}else if(req.session.user.role.toLowerCase() !== 'primary user'){
let msg = `${req.session.user.firstName}, You do not have permission to view this page. only primary user can view this, your current role is of user`
return res.status(403).render('error', {title: "error", error: `<div id="error" class="error" > ${msg}</div>`});
}else{
next();
}
},
logoutMiddleware: (req, res, next)=>{
if(!req.session.user){
return res.redirect('/login')
}else{
next();
}
},
loggingMiddleware: (req, res, next)=>{
const curentTime = new Date().toUTCString();
const requestMethod = req.method;
const requestRoute = req.originalUrl;
const userAuthentication = req.session.user ? "Authenticated User" : "Non-Authenticated User";
//[Sun, 14 Apr 2019 23:56:06 GMT]: GET / (Non-Authenticated User)
console.log(`[${curentTime}]: ${requestMethod} ${requestRoute} (${userAuthentication})`);
next();
}
}
export default middlewareMethods;