-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.js
43 lines (39 loc) · 1.17 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
const {educationSchema, experienceSchema, projectSchema} = require("./schemas.js");
const ExpressError = require("./utils/ExpressError");
module.exports.isLoggedIn = (req, res, next) => {
if(!req.isAuthenticated()) {
req.session.returnTo = req.originalUrl;
return res.redirect("/login");
}
next();
}
module.exports.validateEducation = (req, res, next) => {
const {error} = educationSchema.validate(req.body);
if (error) {
const message = error.details.map(el => el.message).join(",");
throw new ExpressError(message, 400);
}
else {
next();
}
}
module.exports.validateExperience = (req, res, next) => {
const {error} = experienceSchema.validate(req.body);
if (error) {
const message = error.details.map(el => el.message).join(",");
throw new ExpressError(message, 400);
}
else {
next();
}
}
module.exports.validateProject = (req, res, next) => {
const {error} = projectSchema.validate(req.body);
if (error) {
const message = error.details.map(el => el.message).join(",");
throw new ExpressError(message, 400);
}
else {
next();
}
}