summaryrefslogtreecommitdiffstats
path: root/protoype/router/user.js
diff options
context:
space:
mode:
Diffstat (limited to 'protoype/router/user.js')
-rw-r--r--protoype/router/user.js69
1 files changed, 69 insertions, 0 deletions
diff --git a/protoype/router/user.js b/protoype/router/user.js
new file mode 100644
index 0000000..4215054
--- /dev/null
+++ b/protoype/router/user.js
@@ -0,0 +1,69 @@
+/*
+ * user.js
+ *
+ * routing handlers for a user
+ *
+ */
+
+var util = require('util');
+var db = require('../mydb.js');
+
+var connected_clients = {};
+
+function create_get(req, res, next) {
+}
+
+function create_post(req, res, next) {
+ db.users.save({tag: req.param('tag'), id: req.param('id'), status: "offline",
+ vehicle: {make: req.param('make'), model: req.param('model'), year: req.param('year'),
+ desc: req.param('desc')}, userinfo: {sig: req.param('sig')}, location: {loc: req.param('loc')},
+ stats: {matches: 0, won: 0, lost: 0}},
+ function(err, thing) {
+ if (err || !thing)
+ util.log('[create] error saving');
+ else
+ util.log('[create] successfully saved');
+ });
+ res.redirect('/');
+}
+
+function login_get(req, res, next) {
+}
+
+/*
+ * test drive: curl -d 'tag=unclescotty&id=brotato' localhost:8081/login
+ *
+ */
+function login_post(req, res, next) {
+ db.users.find({tag: req.param('tag')}, function(err, thing) {
+ if (err || !thing || thing.length == 0) {
+ util.log('[login] user does not exist');
+ res.send('user does not exist\n', 403);
+ }
+ else {
+ /* util.log('[login] retrived user: ' + util.inspect(thing)); */
+ if (req.param('id') === thing[0].id) { /* insert md5 hashing here */
+ util.log('[login] ' + thing[0].tag + ' authenticated');
+ db.users.update({tag: req.param('tag')}, {$set: {status: 'online'}}, function(err, updated) {
+ if (err || !updated)
+ util.log('[login] failed to set status to online');
+ });
+ /* real deal? */
+ connected_clients[thing[0].tag] = {ip: res.connection.myip, port: res.connection.myport};
+ res.send('successfully logged in\n', 200);
+ }
+ else {
+ util.log('[login] could not authenticate');
+ res.send('could not authenticate\n', 401);
+ }
+ }
+ });
+}
+
+module.exports = {
+ create_get: create_get,
+ create_post: create_post,
+ login_get: login_get,
+ login_post: login_post
+};
+