-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
81 lines (73 loc) · 2.29 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
const ExpressError = require('./utils/ExpressError');
module.exports.isLoggedIn = (request, response, next) => {
if (!request.isAuthenticated()) {
request.session.returnTo = request.originalUrl
request.flash('error', 'Ypu need to sign in first');
return response.redirect('/login')
}
next();
}
module.exports.storeReturnTo = (req, res, next) => {
if (req.session.returnTo) {
res.locals.returnTo = req.session.returnTo;
}
next();
}
const BaseJoi = require('joi');
const sanitizeHtml = require('sanitize-html');
const extension = (joi) => ({
type: 'string',
base: joi.string(),
messages: {
'string.escapeHTML': '{{#label}} must not include HTML!'
},
rules: {
escapeHTML: {
validate(value, helpers) {
const clean = sanitizeHtml(value, {
allowedTags: [],
allowedAttributes: {},
});
if (clean !== value) return helpers.error('string.escapeHTML', { value })
return clean;
}
}
}
});
const Joi = BaseJoi.extend(extension)
module.exports.validateCampground = (request, response, next) => {
const campgroundSchema = Joi.object({
campground: Joi.object({
title: Joi.string().required().escapeHTML(),
price: Joi.number().required().min(0),
// image: Joi.string().required(),
location: Joi.string().required().escapeHTML(),
description: Joi.string().required().escapeHTML()
}).required(),
deleteImages : Joi.array()
})
const { error } = campgroundSchema.validate(request.body);
if (error) {
const msg = error.details.map(el => el.message).join(',')
throw new ExpressError(msg, 400)
}
else {
next();
}
}
module.exports.validateReview = (request, response, next) => {
const reviewSchema = Joi.object({
review: Joi.object({
body: Joi.string().required().escapeHTML(),
rating: Joi.number().required(),
}).required()
})
const { error } = reviewSchema.validate(request.body);
if (error) {
const msg = error.details.map(el => el.message).join(',')
throw new ExpressError(msg, 400)
}
else{
next();
}
}