-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
168 lines (153 loc) · 4.92 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
export const userMiddleware = (protectedUserRoutes) => {
return (req, res, next) => {
const isLoggedIn = req.session.user;
const fullPath = req.baseUrl + req.path;
const isAcenterLoggedIn = req.session.acenter;
// if (!req.xhr) {
// return res.redirect("/404Page");
// }
if (
req.url !== "/login-page" &&
req.url !== "/signup-page" &&
req.url !== "/login" &&
req.url !== "/signup"
) {
if (!req.session) {
return res.redirect("/users/login-page");
}
if (!req.session.user) {
return res.redirect("/users/login-page");
}
}
if (
protectedUserRoutes.some(
(route) => fullPath === "/users" + route
) &&
!isLoggedIn
) {
res.redirect("/users/login-page");
return;
}
if (isAcenterLoggedIn) {
res.status(404).render("404Page", {
title: "404 Page",
error: "You are already logged in as an adoption center.",
});
return;
}
next();
};
};
export const acenterMiddleware = (protectedAcenterRoutes) => {
return (req, res, next) => {
const isLoggedIn = req.session.acenter;
const fullPath = req.baseUrl + req.path;
const isUserLoggedIn = req.session.user;
// console.log("Full path:", fullPath);
// console.log("Is acenter logged in:", isLoggedIn);
if (
req.url !== "/login" &&
req.url !== "/signup" &&
req.url !== "/login-page" &&
req.url !== "/signup-page"
) {
if (!req.session) {
// console.log("No session found");
return res.redirect("/acenters/login-page");
}
if (!req.session.acenter) {
// console.log("No acenter found in session");
return res.redirect("/acenters/login-page");
}
}
if (
protectedAcenterRoutes.some(
(route) => fullPath === "/acenters" + route
) &&
!isLoggedIn
) {
// console.log("Protected route and not logged in");
res.redirect("/acenters/login-page");
return;
}
if (isUserLoggedIn) {
// console.log("User is logged in");
res.status(404).render("404Page", {
title: "404 Page",
error: "You are already logged in as a user.",
});
return;
}
// console.log("Continuing to next middleware");
next();
};
};
export const chatMiddleware = (protectedChatRoutes) => {
return (req, res, next) => {
//console.log(req.session, req.xhr);
const isUserLoggedIn = req.session.user;
const isAcenterLoggedIn = req.session.acenter;
const fullPath = req.baseUrl + req.path;
// console.log(`${isUserLoggedIn} ${isAcenterLoggedIn} ${fullPath}`);
console.log(protectedChatRoutes.some(
(route) =>
console.log(route)
//fullPath === "/chats" + route
))
if(!isUserLoggedIn && !isAcenterLoggedIn){
return res.redirect("/users/login-page");
}
// if (!isUserLoggedIn &&
// !isAcenterLoggedIn &&
// protectedChatRoutes.some(
// (route) => fullPath === "/chats" + route
// )
// ) {
// return res.redirect("/404Page");
// }
if (
protectedChatRoutes.some((route) => fullPath === "/chats" + route)
) {
if (!req.xhr) {
return res.redirect("/404Page");
}
}
if (isUserLoggedIn) {
if (req.path.split("/")[2] !== req.session.user._id) {
return res.redirect("/404Page");
}
}
if (isAcenterLoggedIn) {
if (req.path.split("/")[2] !== req.session.acenter._id) {
return res.redirect("/acenters/login-page");
}
}
console.log("here");
next();
};
};
export const redirectToScrollerIfLoggedIn = () => {
return (req, res, next) => {
// console.log("req.session: ", req.session);
if (req.session.user) {
return res.redirect(`/users/scroller/${req.session.user._id}`);
}
if (req.session.acenter) {
res.redirect(`/acenters/ac-dashboard/${req.session.acenter._id}`);
return;
}
next();
};
};
export function requireAcenterLogin(req, res, next) {
if (!req.session.acenter) {
return res.redirect("/acenters/login-page");
}
next();
}
export function requireUserLogin(req, res, next) {
if (!req.session.user) {
return res.redirect("/users/login-page");
}
next();
}