-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
40 lines (37 loc) · 930 Bytes
/
utils.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
var fs = require('fs');
module.exports = {
ensureAuthenticated: function(req, res, next) {
if (req.isAuthenticated()) {
return next();
} else {
req.flash('error_msg', 'You are not logged in.');
res.redirect('/');
}
},
redirectIfLoggedIn: function(req, res, next) {
if (req.isAuthenticated()) {
res.redirect('/scout')
} else {
return next();
}
},
ensureAdmin: function(req, res, next) {
if (req.isAuthenticated() && res.locals.user.admin) {
return next();
} else {
req.flash('error_msg', 'You are not an admin.');
res.redirect('/scout');
}
},
getCurrentEvent: function() {
var buffer = fs.readFileSync('./config/state.db');
var json = JSON.parse(buffer.toString());
return json["current_event"];
},
average: function(array) {
if (array.length == 0) return 0;
var sum = 0;
for (var i = 0; i < array.length; i ++) sum += array[i];
return sum /= array.length;
}
};