-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
52 lines (37 loc) · 1.12 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
const{ productschema, reviewschema } = require('./schema');
const validateProduct =(req,res,next)=>{
let {name,img,price,desc} =req.body;
console.log(name,img,price,desc);
const {error}= productschema.validate({name,img,price,desc})
if(error){return res.render('./partials/error')
}
next();
}
const validateReview =(req,res,next)=>{
let {rating,comment} =req.body;
console.log(rating,comment);
const {error}= reviewschema.validate({rating,comment})
if(error){return res.render('./partials/error')
}
next();
}
const isLoggedin = (req,res,next)=>{
if(!req.isAuthenticated()){
console.log('You are not logged in' );
return res.redirect('/login');
}
next();
}
const isSeller=(req,res,next)=>{
if(!req.user.role){
return res.redirect('/products');
}
else if(req.user.role!== 'seller'){
req.flash('error' , 'you do not have permission');
return res.redirect('/products');
}
next();
};
const isProductAuthor = (req,res,next)=>{
}
module.exports ={isSeller,validateProduct,validateReview,isLoggedin}