From 5f52ab39bf1fde69baa55922b89e699a3fbfbafa Mon Sep 17 00:00:00 2001 From: joates Date: Sat, 20 Sep 2014 18:24:37 +0100 Subject: [PATCH] added db storage (match email addresses to tokens) --- server.js | 20 ++++++++++++++------ src/ip-trace.js | 8 ++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 src/ip-trace.js diff --git a/server.js b/server.js index 0548b92..87ae80d 100644 --- a/server.js +++ b/server.js @@ -5,6 +5,8 @@ var server = require('http').createServer(handler) , rn = require('./src/rng') , fs = require('fs') , re = new RegExp('\.js$', 'i') + , ip = require('./src/ip-trace') + , db = require('level')('./db/squatconf', { valueEncoding: 'json' }) , port = process.env.PORT || /*80*/ 8000 function handler(req, res) { @@ -34,13 +36,17 @@ function handler(req, res) { obj.token = rn() obj.verified = false obj.events = { paris: params.query.paris ? true : false } - obj.trace = (req.headers['x-forwarded-for'] || '').split(',') - || [ req.connection.remoteAddress ] + obj.trace = ip(req) - var db = require('level')('./db/squatconf', { valueEncoding: 'json' }) db.put(email, obj, function(err) { - if (err) cb(err) + if (err) throw err + // else.. db updated OK + db.get(email, function (err, value) { + if (err) return console.error('Ooops!', err) + + console.log('> '+ email, value) + }) }) var nodemailer = require('nodemailer') @@ -53,14 +59,16 @@ function handler(req, res) { from : config.email.from , to : email , subject: config.email.subject - , text : config.email.bodyText.replace(/\%link\%/, link) + //, text : config.email.bodyText.replace(/\%link\%/, link) } - +console.log('mockmail sent...', opts) + /* transporter.sendMail(opts, function(err, data) { if (err) throw err // validation email sent console.log('email sent..', data) }) + */ } res.statusCode = 302 diff --git a/src/ip-trace.js b/src/ip-trace.js new file mode 100644 index 0000000..f34bbe0 --- /dev/null +++ b/src/ip-trace.js @@ -0,0 +1,8 @@ +module.exports = function(req) { + if (req.headers['x-forwarded-for']) { + return req.headers['x-forwarded-for'].split(',') + } else { + return [ req.connection.remoteAddress ] + } +} +