diff --git a/lib/api/v1/account.js b/lib/api/v1/account.js index c45f180..11df746 100644 --- a/lib/api/v1/account.js +++ b/lib/api/v1/account.js @@ -1,9 +1,10 @@ -var crypto = require('crypto'), - Hapi = require('hapi'), - db = require('../../db'); +var crypto = require('crypto'); +var Hapi = require('hapi'); +var db = require('../../db.js'); +var util = require('../../util.js'); -var B = Hapi.Types.Boolean, - S = Hapi.Types.String; +var B = Hapi.Types.Boolean; +var S = Hapi.Types.String; module.exports = { method: 'POST', @@ -13,20 +14,19 @@ module.exports = { mode: 'none' }, description: 'Stage a new account', - schema: { - email: S(), - pass: S(), - newsletter: B() - }, - response: { - success: B() + validate: { + schema: { + email: S().email().required(), + pass: S().required(), + newsletter: B() + } } } }; function handler(request) { db.stageAccount(request.payload, function(err) { - if (err) request.reply(Hapi.Error.internal("error staging account")); + if (err) return request.reply(Hapi.Error.badRequest("Could not create account: " + err)); request.reply({ success: true }); diff --git a/lib/api/v1/context.js b/lib/api/v1/context.js index 2619298..7a36ae6 100644 --- a/lib/api/v1/context.js +++ b/lib/api/v1/context.js @@ -21,7 +21,7 @@ module.exports = { function handler(request) { crypto.randomBytes(32, function(err, bytes) { - if (err) return Hapi.Error.internal("error attaining entropy"); + if (err) return request.reply(Hapi.Error.internal("error attaining entropy")); request.reply({ server_time: Math.round((new Date()).getTime() / 1000), entropy: bytes.toString('base64') diff --git a/lib/db/json.js b/lib/db/json.js index 85e8443..cc9833a 100644 --- a/lib/db/json.js +++ b/lib/db/json.js @@ -30,6 +30,7 @@ module.exports = { staged: false }; setTimeout(function() { + if (db[data.email]) return cb("That email has already been used"); db[data.email] = account; cb(null); }, 0);