-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
37 lines (33 loc) · 1.03 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
const {suggestionSchema, notesLinkSchema} = require("./schema.js");
module.exports.validateSuggestion = (req, res, next) => {
let {error} = suggestionSchema.validate(req.body);
if(error){
let errMsg = error.details.map((el) => el.message).join(",");
throw new ExpressError(400, errMsg);
} else{
next();
}
}
module.exports.validateNoteLink = (req, res, next) => {
let {error} = notesLinkSchema.validate(req.body);
if(error){
let errMsg = error.details.map((el) => el.message).join(",");
throw new ExpressError(400, errMsg);
} else{
next();
}
}
module.exports.isLoggedIn = (req, res, next) => {
if(!req.isAuthenticated()){
req.session.redirectUrl = req.originalUrl;
req.flash("error", "You must login to view the Content!");
return res.redirect("/login");
}
next();
}
module.exports.saveRedirectUrl = (req, res, next) => {
if(req.session.redirectUrl){
res.locals.redirectUrl = req.session.redirectUrl;
}
next();
}