-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
25 lines (22 loc) · 862 Bytes
/
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
// This middleware we're using to check if the user is logged in or not. If not, then we will redirect him to the login page.
// Now, we will use this middleware when user tries to add/delete a listing, edit a listing or comment review.
module.exports.isLoggedIn = (req,res,next) =>
{
if(!req.isAuthenticated()) // It will check whether the user is logged in or not.
{
// if our user was not logged in, we will save the information where user wants to redirect
// after he logs in.
req.session.redirectUrl = req.originalUrl;
req.flash("error", "You must be logged in to use this function!");
return res.redirect("/login");
}
next();
}
module.exports.saveRedirectUrl = (req,res,next) =>
{
if(req.session.redirectUrl)
{
res.locals.redirectUrl = req.session.redirectUrl;
}
next();
}