[TASK] Move /user API handler to src/User.api.js.

This commit is contained in:
Jan Philipp Timme 2013-09-16 19:01:03 +02:00
parent 8aff2adfa5
commit f18dd39a36
2 changed files with 70 additions and 55 deletions

57
app.js
View File

@ -15,6 +15,7 @@ var settings = require("./src/Settings.js");
//load api handler //load api handler
var sessionAPIHandler = require("./src/Session.api.js"); var sessionAPIHandler = require("./src/Session.api.js");
var userAPIHandler = require("./src/User.api.js");
//initialize couch connector //initialize couch connector
cradle.setup(settings.couchdb); cradle.setup(settings.couchdb);
@ -70,61 +71,7 @@ app.use("/", express.static(__dirname + '/static'));
app.use("/session", new sessionAPIHandler(db)); app.use("/session", new sessionAPIHandler(db));
//API: /user //API: /user
app.use("/user", function(req, res) { app.use("/user", new userAPIHandler(db));
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);
}
});
//define 404 for everything else or 500 on error (ugly but i think it's useful) //define 404 for everything else or 500 on error (ugly but i think it's useful)
app.use(function(err, req, res, next) { app.use(function(err, req, res, next) {

68
src/User.api.js Normal file
View File

@ -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;