From f18dd39a3696d52fc3c37a8376933f7f6f00cec8 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Mon, 16 Sep 2013 19:01:03 +0200 Subject: [PATCH] [TASK] Move /user API handler to src/User.api.js. --- app.js | 57 ++--------------------------------------- src/User.api.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 55 deletions(-) create mode 100644 src/User.api.js diff --git a/app.js b/app.js index e3f3bff..86b5415 100644 --- a/app.js +++ b/app.js @@ -15,6 +15,7 @@ var settings = require("./src/Settings.js"); //load api handler var sessionAPIHandler = require("./src/Session.api.js"); +var userAPIHandler = require("./src/User.api.js"); //initialize couch connector cradle.setup(settings.couchdb); @@ -70,61 +71,7 @@ app.use("/", express.static(__dirname + '/static')); app.use("/session", new sessionAPIHandler(db)); //API: /user -app.use("/user", function(req, res) { - res.setHeader("Content-Type", "application/json"); - if(req.method == "PUT") { - var params = req.body; - if(tools.reqParamsGiven(["username", "password", "email"], params) == false) { - res.send(500, JSON.stringify({ - "success": false, - "err": "This method needs username, password and email!" - })); - return; - } - //check if user already exists - db.get(params.username, function (err, doc) { - if(!err || err.error != "not_found" || err.reason != "missing") { - res.send(200, JSON.stringify({ - "success": false, - "err": "Username already taken!" - })); - return; - } - //get: {"0":{"error":"not_found","reason":"missing"}} - scrypt.passwordHash(params.password, 10, function(err, pwHash) { - var userDoc = { - "_id": params.username, - "auth": pwHash, - "email": params.email, - "type": "user" - }; - db.save(userDoc._id, userDoc, function(err, result) { - if(err) { - res.send(200, JSON.stringify({ - "success": false, - "err": err - })); - } else { - res.send(200, JSON.stringify({ - "success": true - })); - } - }); - }); - }); - } - if(req.method == "GET") { - res.send(200, JSON.stringify(req.session.data.user)); - } - if(req.method == "POST") { - console.log(req); - } - if(req.method == "DELETE") { - //verify credentials before erasing all data - console.log(req); - - } -}); +app.use("/user", new userAPIHandler(db)); //define 404 for everything else or 500 on error (ugly but i think it's useful) app.use(function(err, req, res, next) { diff --git a/src/User.api.js b/src/User.api.js new file mode 100644 index 0000000..3f3f678 --- /dev/null +++ b/src/User.api.js @@ -0,0 +1,68 @@ +var scrypt = require("scrypt"); +var tools = require("./Tools.js"); + +var constructor = function(db) { + var db = db; + + var handler = function(req, res) { + res.setHeader("Content-Type", "application/json"); + + if(req.method == "PUT") { + var params = req.body; + if(tools.reqParamsGiven(["username", "password", "email"], params) == false) { + res.send(500, JSON.stringify({ + "success": false, + "err": "This method needs username, password and email!" + })); + return; + } + //check if user already exists + db.get(params.username, function (err, doc) { + if(!err || err.error != "not_found" || err.reason != "missing") { + res.send(200, JSON.stringify({ + "success": false, + "err": "Username already taken!" + })); + return; + } + scrypt.passwordHash(params.password, 10, function(err, pwHash) { + var userDoc = { + "_id": params.username, + "auth": pwHash, + "email": params.email, + "type": "user" + }; + db.save(userDoc._id, userDoc, function(err, result) { + if(err) { + res.send(200, JSON.stringify({ + "success": false, + "err": err + })); + } else { + res.send(200, JSON.stringify({ + "success": true + })); + } + }); + }); + }); + } + + if(req.method == "GET") { + res.send(200, JSON.stringify(req.session.data.user)); + } + + if(req.method == "POST") { + console.log(req); + } + + if(req.method == "DELETE") { + //verify credentials before erasing all data + console.log(req); + } + }; + + return handler; +}; + +module.exports = constructor; \ No newline at end of file