From 72e3d4179ea423d5ec489bfe5e27006234f867e1 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Mon, 18 May 2015 17:51:20 +0200 Subject: [PATCH] Init session stuff --- controllers/prototype/index.js | 19 +++++++++++++++++++ package.json | 4 +++- server.js | 19 ++++++++++++++----- static/js/Logger.js | 2 +- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/controllers/prototype/index.js b/controllers/prototype/index.js index 2190458..b0c022a 100644 --- a/controllers/prototype/index.js +++ b/controllers/prototype/index.js @@ -1,3 +1,21 @@ +var pg = require('pg'); +var pgc = require('../../private.js'); + +var createNewId = function() { + var value; + pg.connect(pgc.url, function(err, client, release) { + client.query( + "INSERT INTO users(name) VALUES('anonymous'); SELECT currval('users_id_seq');", + [], + function(err, result) { + value = result.rows[0].currval; + release(); + return value; + } + ); + }); +} + module.exports.index = function(req, res) { res.setHeader('Content-Type', 'text/html'); @@ -7,6 +25,7 @@ module.exports.index = function(req, res) { } module.exports.arrows = function(req, res) { + // req.session.user_id = createNewId(); res.setHeader('Content-Type', 'text/html'); res.locals.cameraStyle = 'arrows'; diff --git a/package.json b/package.json index a51f9b6..bae38a2 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "express": "4.0", "jade": "1.9.2", "pg": "4.3.0", - "body-parser": "1.12.4" + "body-parser": "1.12.4", + "express-session": "1.11.2", + "cookie-parser": "1.3.4" }, "repository": { "type": "git", diff --git a/server.js b/server.js index 597f600..62fcb65 100644 --- a/server.js +++ b/server.js @@ -3,15 +3,24 @@ var express = require('express'); var jade = require('jade'); var pg = require('pg'); -// pg conf -var pgc = require('./private'); +// secret variables +var secret = require('./private'); var app = express(); -var bodyParser = require('body-parser') +var bodyParser = require('body-parser'); +var session = require('express-session'); +var cookieParser = require('cookie-parser'); var urls = require('./urls'); app.set('view engine', 'jade'); +app.use(cookieParser()); +app.use(session({ + saveUninitialized: true, + resave: true, + secret: secret.secret +})); + app.use(bodyParser.text()); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); @@ -29,10 +38,10 @@ require('./lib/boot')(app, { verbose: !module.parent }); app.use('/static', express.static('static')); app.post('/post', function(req, res) { - var user_id = req.body.user_id; + var user_id = 1; var arrow_id = req.body.arrow_id; - pg.connect(pgc.url, function(err, client, release) { + pg.connect(secret.url, function(err, client, release) { client.query( "INSERT INTO arrowclicked(user_id, arrow_id) VALUES($1,$2);", [user_id, arrow_id], diff --git a/static/js/Logger.js b/static/js/Logger.js index 57acee5..081c3d6 100644 --- a/static/js/Logger.js +++ b/static/js/Logger.js @@ -8,7 +8,7 @@ BD.Event.ArrowClicked.prototype.send = function() { var data = {}; - data.user_id = this.user_id; + data.session_id = session_id; data.arrow_id = this.arrow_id; var xhr = new XMLHttpRequest();