-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
48 lines (41 loc) · 1.26 KB
/
util.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
43
44
45
46
47
48
var util = {};
util.isLoggedin = function(req, res, next){
if(req.isAuthenticated()){
next();
} else {
req.flash("errors", {login:"Please login first"});
res.redirect("/login");
}
}
util.noPermission = function(req, res){
req.flash("errors", {login:"You don't have permission"});
req.logout();
res.redirect("/login");
}
util.parseError = function(errors){
var parsed = {};
if(errors.name == 'ValidationError'){
for(var name in errors.errors){
var validationError = errors.errors[name];
parsed[name] = { message:validationError.message };
}
} else if(errors.code == "11000" && errors.errmsg.indexOf("username") > 0) {
parsed.username = { message:"This username already exists!" };
} else {
parsed.unhandled = JSON.stringify(errors);
}
return parsed;
}
util.getDate = function(dateObj){
if(dateObj instanceof Date)
return dateObj.getFullYear() + "-" + get2digits(dateObj.getMonth()+1)+ "-" + get2digits(dateObj.getDate());
}
util.getTime = function(dateObj){
if(dateObj instanceof Date)
return get2digits(dateObj.getHours()) + ":" + get2digits(dateObj.getMinutes())+ ":" + get2digits(dateObj.getSeconds());
}
module.exports = util;
// private functions
function get2digits (num){
return ("0" + num).slice(-2);
}