-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
126 lines (100 loc) · 3.14 KB
/
Copy pathmiddleware.js
File metadata and controls
126 lines (100 loc) · 3.14 KB
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
const Listing = require("./models/listing");
const Review = require("./models/review");
const ExpressError = require("./utils/ExpressError");
// LOGIN CHECK
module.exports.isLoggedIn = (req, res, next) => {
if (!req.isAuthenticated()) {
if (req.method === "GET") {
req.session.redirectUrl = req.originalUrl;
}
req.flash("error", "You must be logged in!");
return res.redirect("/login");
}
return next();
};
// SAVE REDIRECT
module.exports.saveRedirect = (req, res, next) => {
// Query string se bhi redirect lo
if (req.query.redirect) {
req.session.redirectUrl = req.query.redirect;
}
if (req.session.redirectUrl) {
res.locals.redirectUrl = req.session.redirectUrl;
delete req.session.redirectUrl;
}
return next();
};
// ================= OWNER CHECK =================
module.exports.isOwner = async (req, res, next) => {
try {
let { id } = req.params;
let listing = await Listing.findById(id);
if (!listing) {
req.flash("error", "Listing not found!");
return res.redirect("/listings");
}
if (!req.user || !listing.owner.equals(req.user._id)) {
req.flash("error", "You are not the owner!");
return res.redirect(`/listings/${id}`);
}
return next();
} catch (err) {
return next(err);
}
};
// REVIEW AUTHOR CHECK
module.exports.isReviewAuthor = async (req, res, next) => {
try {
let { reviewId, id } = req.params;
let review = await Review.findById(reviewId);
if (!review) {
req.flash("error", "Review not found!");
return res.redirect(`/listings/${id}`);
}
if (!req.user || !review.author.equals(req.user._id)) {
req.flash("error", "You are not the author of this review!");
return res.redirect(`/listings/${id}`);
}
return next();
} catch (err) {
return next(err);
}
};
// =VALIDATE LISTING
module.exports.validateListing = (req, res, next) => {
let { listing } = req.body;
if (!listing) {
req.flash("error", "Listing data is required");
return res.redirect("/listings");
}
if (!listing.title || listing.title.trim().length < 3) {
req.flash("error", "Title must be at least 3 characters");
return res.redirect(
req.method === "POST" ? "/listings/new" : `/listings/${req.params.id}/edit`
);
}
if (!listing.price || isNaN(Number(listing.price))) {
req.flash("error", "Valid price is required");
return res.redirect(
req.method === "POST" ? "/listings/new" : `/listings/${req.params.id}/edit`
);
}
if (req.method === "POST" && !req.file) {
req.flash("error", "Image is required");
return res.redirect("/listings/new");
}
return next();
};
// VALIDATE REVIEW
module.exports.validateReview = (req, res, next) => {
let { review } = req.body;
if (!review || !review.comment || review.comment.trim().length === 0) {
req.flash("error", "Review comment is required");
return res.redirect(`/listings/${req.params.id}`);
}
if (!review.rating || review.rating < 1 || review.rating > 5) {
req.flash("error", "Review rating must be between 1 and 5");
return res.redirect(`/listings/${req.params.id}`);
}
return next();
};