|
|
|
@ -1,7 +1,8 @@
|
|
|
|
|
var express = require('express'),
|
|
|
|
|
WebServer = express(),
|
|
|
|
|
server = require('http').createServer(WebServer),
|
|
|
|
|
RedisStore = require('connect-redis')(express);
|
|
|
|
|
RedisStore = require('connect-redis')(express),
|
|
|
|
|
path = require('path'),
|
|
|
|
|
config = require('../config.js');
|
|
|
|
|
|
|
|
|
|
(function(app) {
|
|
|
|
@ -26,8 +27,10 @@ var express = require('express'),
|
|
|
|
|
|
|
|
|
|
// Middlewares
|
|
|
|
|
app.use(express.favicon()); // 2 args: string path and object options (i.e. expire time etc)
|
|
|
|
|
app.use(express.static(path.join(__dirname, '../', 'public')));
|
|
|
|
|
app.use(express.bodyParser()); // Puts POST vars in request.body
|
|
|
|
|
app.use(express.cookieParser()); // If you want to parse cookies (res.cookies)
|
|
|
|
|
app.use(express.compress());
|
|
|
|
|
app.use(express.session({
|
|
|
|
|
store: new RedisStore({
|
|
|
|
|
ttl: 60*60*24*14
|
|
|
|
@ -36,20 +39,34 @@ var express = require('express'),
|
|
|
|
|
key: 'express.sid'
|
|
|
|
|
}));
|
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
|
if (global.uid === undefined) {
|
|
|
|
|
console.log('info: [Auth] First load, retrieving uid...');
|
|
|
|
|
global.modules.user.get_uid_by_session(req.sessionID, function(uid) {
|
|
|
|
|
global.uid = uid;
|
|
|
|
|
if (global.uid !== null) console.log('info: [Auth] uid ' + global.uid + ' found. Welcome back.');
|
|
|
|
|
else console.log('info: [Auth] No login session found.');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// (Re-)register the session as active
|
|
|
|
|
global.modules.user.active.register(req.sessionID);
|
|
|
|
|
} else {
|
|
|
|
|
console.log('info: [Auth] Ping from uid ' + global.uid);
|
|
|
|
|
// Don't bother with session handling for API requests
|
|
|
|
|
if (!/^\/api\//.test(req.url)) {
|
|
|
|
|
if (req.session.uid === undefined) {
|
|
|
|
|
console.log('info: [Auth] First load, retrieving uid...');
|
|
|
|
|
global.modules.user.get_uid_by_session(req.sessionID, function(uid) {
|
|
|
|
|
if (uid !== null) {
|
|
|
|
|
req.session.uid = uid;
|
|
|
|
|
|
|
|
|
|
global.socket.emit('event:alert', {
|
|
|
|
|
title: 'Welcome ' + user.username,
|
|
|
|
|
message: 'You have successfully logged in.',
|
|
|
|
|
type: 'notify',
|
|
|
|
|
timeout: 2000
|
|
|
|
|
});
|
|
|
|
|
} else req.session.uid = 0;
|
|
|
|
|
|
|
|
|
|
if (req.session.uid) console.log('info: [Auth] uid ' + req.session.uid + ' found. Welcome back.');
|
|
|
|
|
else console.log('info: [Auth] No login session found.');
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// console.log('SESSION: ' + req.sessionID);
|
|
|
|
|
// console.log('info: [Auth] Ping from uid ' + req.session.uid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// (Re-)register the session as active
|
|
|
|
|
global.modules.user.active.register(req.sessionID);
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
// Dunno wtf this does
|
|
|
|
@ -64,7 +81,6 @@ var express = require('express'),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// need a proper way to combine these two routes together
|
|
|
|
|
app.get('/topics/:topic_id', function(req, res) {
|
|
|
|
|
global.modules.topics.generate_topic_body(function(topic_body) {
|
|
|
|
@ -88,6 +104,7 @@ var express = require('express'),
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
res.send('{}');
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -97,8 +114,11 @@ var express = require('express'),
|
|
|
|
|
|
|
|
|
|
app.get('/logout', function(req, res) {
|
|
|
|
|
console.log('info: [Auth] Session ' + res.sessionID + ' logout (uid: ' + global.uid + ')');
|
|
|
|
|
global.modules.user.logout(function(logout) {
|
|
|
|
|
if (logout === true) req.session.destroy();
|
|
|
|
|
global.modules.user.logout(req.sessionID, function(logout) {
|
|
|
|
|
if (logout === true) {
|
|
|
|
|
delete(req.session.uid);
|
|
|
|
|
req.session.destroy();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.send(templates['header'] + templates['logout'] + templates['footer']);
|
|
|
|
@ -124,13 +144,6 @@ var express = require('express'),
|
|
|
|
|
app.get('/403', function(req, res) {
|
|
|
|
|
res.send(templates['header'] + templates['403'] + templates['footer']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
module.exports.init = function() {
|
|
|
|
|
// todo move some of this stuff into config.json
|
|
|
|
|
app.configure(function() {
|
|
|
|
|
app.use(express.static(global.configuration.ROOT_DIRECTORY + '/public'));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}(WebServer));
|
|
|
|
|
|
|
|
|
|
server.listen(config.port);
|
|
|
|
|