From 7c19cf04708f5c8209edceaf96dfcfb9dec71e26 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Mon, 16 Sep 2013 22:02:10 +0200 Subject: [PATCH] [TASK] Implement DELETE /user. --- src/User.api.js | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/User.api.js b/src/User.api.js index 621145a..e7729ad 100644 --- a/src/User.api.js +++ b/src/User.api.js @@ -67,8 +67,39 @@ var constructor = function(db) { } if(req.method == "DELETE") { - //verify credentials before erasing all data - console.log(req); + if(req.session.data.login == false) { + res.send(200, JSON.stringify({ + "success": false, + "err": "You are not logged in!" + })); + return; + } + //check if user document exists + db.get(req.session.data.user._id, function (err, doc) { + console.log(["delete/db.get", arguments]); + if(err && err.error == "not_found" && err.reason == "missing") { + res.send(200, JSON.stringify({ + "success": false, + "err": "User document does not exist!" + })); + return; + } + db.remove(doc._id, doc._rev, function(err, result) { + if(err) { + res.send(200, JSON.stringify({ + "success": false, + "error": "Could not delete user document!" + })); + } else { + //kill session data, too + delete req.session; + //TODO: delete profile document here, too! + res.send(200, JSON.stringify({ + "success": true + })); + } + }); + }); } };