aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/router.js
blob: b757e79a4c4c701de5709f8fe3538d3a2135fa2a (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

var AM = require('./modules/account-manager');
var EM = require('./modules/email-dispatcher');





module.exports = function(app) {
    /*
     * GET home page.
     *
     * '/'
     */

    app.get('/', function(req, res){
        // check if the user's credentials are saved in a cookie
        if (req.cookies.user == undefined || req.cookies.pass == undefined) {
            res.render('index', { title: 'DERS' });
        } else { // appempt automatic login
            AM.autoLogin(req.cookies.user, req.cookies.pass, function(o) {
                if (o != null) {
                    req.session.user = o;
                    res.redirect('/dashboard');
                } else {
                    res.render('index', { title: 'DERS' });
                }
            });
        }
    });

    /*
     * GET TEST PAGE
     *
     * '/test'
     */

    app.get('/test', function(req, res) {
        res.render('test', {
            title: 'test',
            loggedin: false
        });
    });


    /*
     * GET dashboard
     *
     * '/dashboard'
     */

    app.get('/dashboard', function(req, res) {
        if (req.session.user == undefined) {
            if (req.cookies.user == undefined || req.cookies.pass == undefined) {
                res.redirect('/login');
            } else {
                console.log('jeg er inne i elsen');
                AM.autoLogin(req.cookies.user, req.cookies.pass, function(o) {
                    if (o != null) {
                        req.session.user = o;
                        res.redirect('/IJUSTAUTOLOGGEDDEGINN');
                    } else {
                    res.redirect('/');
                    }
                });
            }
           // res.redirect('/HAHAHAHAHAHA');
        } else {
            res.render('dashboard', {
                                        title: 'kanin',
                                        loggedin: true
                                    });
        }
    });



    /*
     * GET login page
     *
     * '/login'
     */

    app.get('/login', function(req, res) {
        if (req.cookies.user == undefined || req.cookies.pass == undefined) {
            res.render('login', { title: 'Logg inn' });
        } else {
            AM.autoLogin(req.cookies.user, req.cookies.pass, function(o) {
                if (o != null) {
                    req.session.user = o;
                    res.redirect('/dashboard');
                } else { res.render('login', { title: 'Logg inn' }); }
            });
        }
    });


    /* POST */

    app.post('/login', function(req, res) {
        AM.manualLogin(req.param('user'), req.param('pass'), function(e, o) {
            if (!o) {
                res.send(e, 400);
            } else {
                req.session.user = o;
                if (req.param('remember-me') == 'on') {
                    res.cookie('user', o.user, { maxAge: 900000000 });
                    res.cookie('pass', o.pass, { maxAge: 900000000 });
                }
                res.redirect('/dashboard');
            }
        });
    });



    /*
     * GET signup page
     *
     * '/signup'
     */

    app.get('/signup', function(req, res) {
        res.render('signup', { title: 'Registrer deg' });
    });


    /* POST */

    app.post('/signup', function(req, res) {
        AM.addNewAccount({
            name    : req.param('name'),
            email   : req.param('email'),
            user    : req.param('user'),
            pass    : req.param('pass'),
            country : req.param('country')
        }, function(e) {
            if (e) {
                res.send(e, 400);
            } else {
                res.send('ok', 200);
            }
        });
    });





    /*
     * ERRORS
     */

    /* 404 */
    app.get('*', function(req, res) {
        res.render('error', { title: '404', text: 'Fant ikke siden' });
    });

    /* 403 on POST */
    app.post('*', function(req, res) {
        res.render('error', { title: '403', text: 'Du har ikke tilgang til denne siden' });
    });


};