summaryrefslogtreecommitdiffstats
path: root/protoype/router/user.js
blob: 4215054791a02624e4504fe50304f5e22bf47ee1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
};