-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
35 lines (31 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
const { taskSchema } = require("./schemas")
const Calender = require('./models/calender')
const ExpressError = require('./utils/ExpressError')
module.exports.isLogin = function (req, res, next) {
if (req.isAuthenticated()) {//check for req.user to determine the authentication
return next();
}
req.flash('error', 'You must be signed in first!');
res.redirect("/login")
}
module.exports.isAuthor = async function (req, res, next) {
try {
let calender = await Calender.findById(req.params.id)
if (calender.author.equals(req.user._id)) return next();
calender = await Calender.findOne({ author: req.user._id })
req.flash('error', "you don't have permission to do that")
res.redirect(`/calender/${calender._id}`)
} catch (e) {
next(new ExpressError(e.msg, 400))
}
}
module.exports.validateTask = function (req, res, next) {
const { error } = taskSchema.validate(req.body);
if (error) {
console.log(error)
const msg = error.details.map(el => el.message).join(',')
throw new ExpressError(msg, 400)
} else {
next();
}
}