-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmiddleware.js
35 lines (31 loc) · 1.04 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 Receiver = require("./models/Receiver")
const Restaurant = require("./models/Restaurant")
module.exports.isLoggedIn = (req, res, next) => {
if (!req.isAuthenticated()) {
req.flash('error', 'Please Login Before Claiming Donation')
return res.redirect('/auth/login')
}
next()
}
module.exports.isReceiver = async (req, res, next) => {
const { id } = req.params
const receiver = await Receiver.findById(id)
if (req.user.email !== receiver.username) {
req.flash('error', 'You do not have required permissions')
return res.redirect('/receivers/' + id)
}
next()
}
module.exports.isRestaurant = async (req, res, next) => {
const { id } = req.params
const restaurant = await Restaurant.findById(id)
if (req.user.email !== restaurant.username) {
req.flash('error', 'You do not have required permissions')
return res.redirect('/restaurants/' + id)
}
next()
}
module.exports.setReturnUrl = (req, res, next) => {
res.locals.returnTo = req.session.returnTo
next()
}