-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.js
42 lines (31 loc) · 1.24 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
var Ticket = require('./models/ticket');
var Notification = require('./models/notification');
var Conversation = require('./models/chat/conversation');
module.exports = {
loadCommon,
checkAuthentication
};
function loadCommon(req, res, next) {
req.user.currentProperties(async function (properties) {
req.properties = properties;
Object.assign(res.locals, {
open_tickets_count: await Ticket.countDocuments({ "property": { $in: properties }}),
active_conversations_count: await Conversation.countDocuments({ "property": { $in: properties },status:'Active'}),
// open_tickets_count: Ticket.find(),
unread_notifications: await Notification.countDocuments({"receivers.account": req.user._id,"receivers.read": false})
});
return next();
});
}
// redirect middleware
function checkAuthentication(req, res, next) {
res.locals.req = req;
// do any checks you want to in here
// CHECK THE USER STORED IN SESSION FOR A CUSTOM VARIABLE
// you can do this however you want with whatever variables you set up
if (req.isAuthenticated()) {
return next();
}
// IF A USER ISN'T LOGGED IN, THEN REDIRECT THEM SOMEWHERE
res.redirect('/login');
}