Init session stuff

This commit is contained in:
Thomas FORGIONE 2015-05-18 17:51:20 +02:00
parent a8ce19e8c3
commit 72e3d4179e
4 changed files with 37 additions and 7 deletions

View File

@ -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) { module.exports.index = function(req, res) {
res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Type', 'text/html');
@ -7,6 +25,7 @@ module.exports.index = function(req, res) {
} }
module.exports.arrows = function(req, res) { module.exports.arrows = function(req, res) {
// req.session.user_id = createNewId();
res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Type', 'text/html');
res.locals.cameraStyle = 'arrows'; res.locals.cameraStyle = 'arrows';

View File

@ -5,7 +5,9 @@
"express": "4.0", "express": "4.0",
"jade": "1.9.2", "jade": "1.9.2",
"pg": "4.3.0", "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": { "repository": {
"type": "git", "type": "git",

View File

@ -3,15 +3,24 @@ var express = require('express');
var jade = require('jade'); var jade = require('jade');
var pg = require('pg'); var pg = require('pg');
// pg conf // secret variables
var pgc = require('./private'); var secret = require('./private');
var app = express(); 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'); var urls = require('./urls');
app.set('view engine', 'jade'); 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.text());
app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); app.use(bodyParser.json());
@ -29,10 +38,10 @@ require('./lib/boot')(app, { verbose: !module.parent });
app.use('/static', express.static('static')); app.use('/static', express.static('static'));
app.post('/post', function(req, res) { app.post('/post', function(req, res) {
var user_id = req.body.user_id; var user_id = 1;
var arrow_id = req.body.arrow_id; 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( client.query(
"INSERT INTO arrowclicked(user_id, arrow_id) VALUES($1,$2);", "INSERT INTO arrowclicked(user_id, arrow_id) VALUES($1,$2);",
[user_id, arrow_id], [user_id, arrow_id],

View File

@ -8,7 +8,7 @@ BD.Event.ArrowClicked.prototype.send = function() {
var data = {}; var data = {};
data.user_id = this.user_id; data.session_id = session_id;
data.arrow_id = this.arrow_id; data.arrow_id = this.arrow_id;
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();