-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.js
82 lines (71 loc) · 1.75 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Generated by CoffeeScript 1.6.1
(function() {
var Response, dbconfig, redis;
redis = require("redis");
Response = require('./vo/Response').Response;
dbconfig = require('./config').db;
exports.authenticateUser = function(req, res) {
var path, result;
result = this.isLoginUser(req);
path = "/login";
if (this.isMobileClient(req)) {
path = "/m/login";
}
if (!result) {
res.redirect(path);
}
return result;
};
exports.isMobileClient = function(req) {
var urlArray;
urlArray = req.path.split("/");
return urlArray[1] === "m";
};
exports.isLoginUser = function(req) {
var _ref;
return ((_ref = req.session) != null ? _ref.userId : void 0) && true;
};
exports.authenticateAdmin = function(req, res) {
var result;
result = this.isAdmin(req);
if (!result) {
if (this.isLoginUser(req)) {
res.redirect('/show');
} else {
res.redirect('/login');
}
}
return result;
};
exports.isAdmin = function(req) {
return this.isLoginUser(req) && req.session.isAdmin === 1;
};
exports.showDBError = function(callback, client, message) {
if (client == null) {
client = null;
}
if (message == null) {
message = '数据库错误';
}
if (client) {
client.quit();
}
return callback(new Response(0, message));
};
exports.createClient = function() {
var client;
client = redis.createClient(dbconfig.port, dbconfig.host);
if (dbconfig.pass) {
client.auth(dbconfig.pass, function(err) {
if (err) {
throw err;
}
});
}
client.on("error", function(err) {
console.log(err);
return client.end();
});
return client;
};
}).call(this);