-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.js
More file actions
52 lines (45 loc) · 1.38 KB
/
Copy pathmiddleware.js
File metadata and controls
52 lines (45 loc) · 1.38 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
const { bookmarkSchema, collectionSchema} = require('./joiSchema.js');
const passport = require('passport');
const ExpressError = require('./utils/expressError.js')
module.exports.isLoggedIn = function(req, res, next){
if(!req.isAuthenticated()){
req.session.returnTo = req.originalUrl
req.flash('warning', 'You must be signed in first');
return res.redirect('/login');
}
next();
};
module.exports.storeReturnTo = (req, res, next) => {
if (req.session.returnTo) {
res.locals.returnTo = req.session.returnTo;
}
next();
};
module.exports.validateBookmark = (req, res, next)=>{
const { error } = bookmarkSchema.validate(req.body);
if(error){
const message = error.details.map(el => el.message).join(',');
throw new ExpressError(message, 400)
}else{
next()
}
};
module.exports.validateCollection = (req, res, next)=>{
const { error } = collectionSchema.validate(req.body);
if(error){
const message = error.details.map(el => el.message).join(',');
throw new ExpressError(message, 400)
}else{
next()
}
};
module.exports.loginAuthenticate = passport.authenticate('local', {
failureFlash : true,
failureRedirect : '/login'
});
module.exports.redirectIfLoggedIn = (req, res, next)=>{
if (req.isAuthenticated()) {
return res.redirect('/');
}
next();
}