-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress-middlewares.js
57 lines (49 loc) · 1.88 KB
/
express-middlewares.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
var SERVER_URL = 'http://192.168.1.10:8383';
MatchProvider = require('./matchprovider').MatchProvider;
var matchProvider= new MatchProvider(GLOBAL.mongo_host, GLOBAL.mongo_port);
// CORS middleware
// see: http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs
exports.allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', SERVER_URL);
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
} else {
next();
}
};
// IsLoggedIn middleware
// if the url is not signup or login, check that the user is loggedIn.
exports.isUserLoggedIn = function(req, res, next) {
var url = req.url;
// Continue if it is a signup or login request
if (('POST' == req.method) &&
(('/users' == url) || ('/users/login' == url) || ('/users/signout' == url))) {
next();
} else { // Not a signup/login request
if(req.session.user_id === undefined) {
res.statusCode = 403;
res.send("User is not logged in - session.user_id === undefined!");
return;
}
matchProvider.isUserLoggedIn(req.session.user_id,
function( error, is_logged_in) {
if(error) {
res.statusCode = 400;
res.send(error);
} else {
if(is_logged_in) {
next();
} else { // Not loggedIn!
req.session.user_id = undefined;
res.statusCode = 403;
res.send("User is not logged in!");
}
}
}
);
}
};