Skip to content

Commit

Permalink
Added session for better communication with server
Browse files Browse the repository at this point in the history
  • Loading branch information
idanstark42 committed Jun 24, 2017
1 parent 23d8519 commit d1bc5f9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pgbuildconfig.json
.cordova/config.json
data/
build/
sessions/
*.sublime*

# bower stuff, include actual files
Expand Down
6 changes: 4 additions & 2 deletions localserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ var configs = [require('./server_modules/slave_mode')];

var middlewareLayers = [express.static(fileSystem.resolve('/src')),
function(req, res, next) { console.log(req.originalUrl); next(); },
require('cookie-parser')(),
require('./server_modules/sessions').middleware,
require('./server_modules/auth').basic(args.basicAuthCreds),
require('./server_modules/cors').middleware,
require('./server_modules/cache').middleware,
require('./server_modules/body_builder').middleware];

var routers = [fileSystem,
views,
var routers = [views,
fileSystem,
require('./server_modules/sessions'),
require('./server_modules/teams'),
require('./server_modules/scores'),
require('./server_modules/challenges')];
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"basic-auth-connect": "~1.0.0",
"express": "^4.10.4",
"mustache-express": "^1.2.4",
"js-beautify": "^1.5.4",
"minimist": "^1.1.0",
"mkdirp": "~0.5.0",
Expand Down
3 changes: 2 additions & 1 deletion server_modules/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ var argv = require('minimist')(process.argv.slice(2));
exports.port = argv.p || 1390;
exports.basicAuthCreds = argv.u;
exports.datadir = argv.d || 'data';
exports.slaveMode = argv.s||false;
exports.slaveMode = argv.s || false;
exports.redisPort = argv.r || 13903;
20 changes: 17 additions & 3 deletions server_modules/sessions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
var args = require('./args');

var session = require('express-session');
var MemoryStore = session.MemoryStore;

var config = {
store: new MemoryStore,
secret: 'some-kind-of-secret-g492p8j2498gnqe9n92-8fj-938f',
resave: false,
resave: true,
saveUninitialized: true
};
var session = require('express-session');

exports.middleware = session(config);
exports.middleware = session(config);

exports.route = function(app) {

app.get('/session', function(req, res) {
console.log("session=" + Object.keys(req.session));
res.json(req.session);
})

};
10 changes: 7 additions & 3 deletions server_modules/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ var fileSystem = require('./file_system');
exports.route = function(app) {

app.get('/',function(req,res) {
res.sendFile(fileSystem.resolve('/src/.html'));
req.session.username = 'user';
console.log(Object.keys(req.session));
res.sendFile(fileSystem.resolve('/src/index.html'));
});

app.get('/',function(req,res) {
res.sendFile(fileSystem.resolve('/src/.html'));
app.get('/admin',function(req,res) {
req.session.username = 'admin';
console.log(Object.keys(req.session));
res.sendFile(fileSystem.resolve('/src/index.html'));
});

};
26 changes: 26 additions & 0 deletions src/js/services/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
define('services/session',[
'services/ng-services',
], function(module) {

return module.service('session', [
'$http',
function($http) {

var session = {};
$http.get('/session').then(function(response) {
for(var key in response) {
session[key] = response[key];
}
});

return {
get: function(key) {
return session[key];
},
keys: function() {
return Object.keys(session);
}
};

}]);
});

0 comments on commit d1bc5f9

Please sign in to comment.