-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.js
More file actions
60 lines (55 loc) · 1.77 KB
/
Copy pathmiddleware.js
File metadata and controls
60 lines (55 loc) · 1.77 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
53
54
55
56
57
58
59
60
const Blog = require("./models/blog.models");
const Comment = require("./models/comment.model");
const ExpressError = require("./utils/ExpressError");
// const {blogSchema} = require("./schema");
module.exports.isLoggedIn = (req, res, next) => {
if (!req.isAuthenticated()) {
//redirectUrl
req.session.redirectUrl = req.originalUrl;
req.flash("error", "You Must Be Logged In");
return res.redirect("/user/login");
}
next();
};
module.exports.saveRedirectUrl = (req, res, next) => {
if (req.session.redirectUrl) {
res.locals.redirectUrl = req.session.redirectUrl;
}
next();
};
// module.exports.isAdmin= async(req,res,next)=>{
// if(res.locals.currUser.role != "admin"){
// req.flash("error","You are Not the Admin");
// return res.redirect(`/`);
// };
// next();
// };
// //Validate Images
// module.exports.validateImage = (req,res,next)=>{
// let {error} = imageSchema.validate(req.body);
// if(error){
// console.log(error);
// let errMsg = error.details.map((el)=>el.message).join(",");
// throw new ExpressError(400,errMsg,error);
// }else{
// next();
// }
// };
module.exports.isOwner = async (req, res, next) => {
let { id } = req.params;
let BlogOwner = await Blog.findById(id);
if (!BlogOwner.owner.equals(res.locals.currUser._id)) {
req.flash("error", "You are Not the Owner Of these Blog");
return res.redirect(`/profile/${id}`);
}
next();
};
module.exports.isCommmentAuthor = async (req, res, next) => {
let { id, commentId } = req.params;
let comment = await Comment.findById(commentId);
if (!comment.author.equals(res.locals.currUser._id)) {
req.flash("error", "You did not Post These Comment");
return res.redirect(`/dashboard/${id}`);
}
next();
};