Browse Source

v0.4.2-rc2 (improved middleware error handling)

2015
joates 7 years ago
parent
commit
e188723c9a
  1. 2
      package.json
  2. 6
      server.js
  3. 38
      src/email-confirm.js
  4. 13
      src/email-submit.js

2
package.json

@ -1,6 +1,6 @@
{
"name": "squatconf-web",
"version": "0.4.1",
"version": "0.4.2",
"description": "website for the squatConf conference",
"main": "server.js",
"scripts": {

6
server.js

@ -18,6 +18,12 @@ if(!fs.existsSync(config.db_path)){
});
}
stack.errorHandler = function error(req, res, err) {
res.statusCode = 302
res.setHeader('Location', '/')
res.end()
}
var app = stack(
route('/email', require('./src/email-submit')(db))
, route('/confirm', require('./src/email-confirm')(db))

38
src/email-confirm.js

@ -5,6 +5,11 @@ module.exports = function(db) {
return function (req, res, next) {
req.resume()
function error(e) {
console.error(e)
return next(e || e.msg || 'ERROR')
}
var params = require('url').parse(req.url, true)
if (params && params.query.email && params.query.token) {
@ -13,25 +18,30 @@ module.exports = function(db) {
, token = sanitize(params.query.token)
db.get(email, function(err, obj) {
if (err) return console.error(err)
if (err) return error(err)
// db read OK..
if (obj && obj.token === token) {
obj.verified = true
obj.trace = obj.trace.concat(ip(req))
db.put(email, obj, function(err) {
if (err) return console.error(err)
// db write OK..
res.statusCode = 302
res.setHeader('Location', '/verified.html')
return res.end()
})
if (obj && ! obj.verified) {
if (obj.token === token) {
obj.verified = true
obj.trace = obj.trace.concat(ip(req))
db.put(email, obj, function(err) {
if (err) return error(err)
// db write OK..
res.statusCode = 302
res.setHeader('Location', '/verified.html')
return res.end()
})
}
} else {
error('email is already verified: '+ email)
}
})
if (next) return next()
} else {
error('invalid input: '+ JSON.stringify(params.query))
}
}
}

13
src/email-submit.js

@ -7,6 +7,11 @@ module.exports = function(db) {
return function (req, res, next) {
req.resume()
function error(e) {
console.error(e)
return next(e || e.msg || 'ERROR')
}
var params = require('url').parse(req.url, true)
if (params && params.query.email) {
@ -22,7 +27,7 @@ module.exports = function(db) {
obj.trace = ip(req)
db.put(email, obj, function(err) {
if (err) return console.error(err)
if (err) return error(err)
// db write OK..
var nodemailer = require('nodemailer')
@ -39,7 +44,7 @@ module.exports = function(db) {
}
transporter.sendMail(opts, function(err, data) {
if (err) return console.error(err)
if (err) return error(err)
// validation email sent
console.log('email sent..', opts)
@ -49,9 +54,9 @@ module.exports = function(db) {
return res.end()
})
})
} else {
error('invalid input: '+ JSON.stringify(params.query))
}
if (next) return next()
}
}
Loading…
Cancel
Save