dashboard/src/User.api.js
Jan Philipp Timme 17e6b0aa70 [TASK] Change http response codes.
I feel this needs a bit more explanation:
I plan to answer all requests with http status code 200 OK
and indicate the success in the actual JSON response.

It is still ugly as hell, but it might be like that for a while.
2013-09-16 21:56:12 +02:00

68 lines
1.5 KiB
JavaScript

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(200, 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;