Repository navigation
Expand file tree
/
Copy pathmiddleware.js
More file actions
103 lines (96 loc) · 2.8 KB
/
Copy pathmiddleware.js
File metadata and controls
103 lines (96 loc) · 2.8 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
const { reviewSchema, landscapeSchema } = require('./schemas.js');
const ExpressError = require('./utils/ExpressError');
const Landscape = require('./models/landscape');
const Review = require('./models/review');
// middleware for validating landscape and review, checking if the user is logged in or not, and if the user is author or not.
/**
* using Joi Schemas for validating landscape
* @param {*} req
* @param {*} res
* @param {*} next
* @throws ExpressError
*/
module.exports.validateLandscape = (req, res, next) => {
const { error } = landscapeSchema.validate(req.body);
if (error) {
const msg = error.details.map(el => el.message).join(',')
throw new ExpressError(msg, 400)
} else {
next();
}
}
/**
* using Joi Schemas for validating review
* @param {*} req
* @param {*} res
* @param {*} next
* @throws ExpressError
*/
module.exports.validateReview = (req, res, next) => {
const { error } = reviewSchema.validate(req.body);
if (error) {
const msg = error.details.map(el => el.message).join(',')
throw new ExpressError(msg, 400)
} else {
next();
}
}
/**
* checking id user is already logged in
* @param {*} req
* @param {*} res
* @param {*} next
*/
module.exports.isLoggedInAlready = (req, res, next) => {
if (req.isAuthenticated()){
req.flash('error', 'You are logged in already');
res.redirect('/');
}
next();
}
/**
* checking if a user is logged in, if not it redirects to '/login' and saves the returnTo address.
* @param {*} req
* @param {*} res
* @param {*} next
*/
module.exports.isLoggedIn = (req, res, next) => {
if (!req.isAuthenticated()) {
if(req.originalMethod == 'GET'){
req.session.returnTo = req.originalUrl
}
req.flash('error', 'You must be signed in first!');
return res.redirect('/login');
}
next();
}
/**
* checking if the current user is the author of the landscape
* @param {*} req
* @param {*} res
* @param {*} next
*/
module.exports.isLandscapeAuthor = async (req, res, next) => {
const { id } = req.params;
const landscape = await Landscape.findById(id);
if (!landscape.author.equals(req.user._id)) {
req.flash('error', 'You do not have permission to do that!');
return res.redirect(`/landscapes/${id}`);
}
next();
}
/**
* checking if the current user is the author of the review
* @param {*} req
* @param {*} res
* @param {*} next
*/
module.exports.isReviewAuthor = async (req, res, next) => {
const { id, reviewId } = req.params;
const review = await Review.findById(reviewId);
if (!review.author.equals(req.user._id)) {
req.flash('error', 'You do not have permission to do that!');
return res.redirect(`/landscapes/${id}`);
}
next();
}