-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
95 lines (84 loc) · 3.04 KB
/
routes.js
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
var Promise = require('bluebird');
var scrypt = require('scrypt-for-humans');
var router = require('express').Router();
var connections = require('./connections.js');
var db = require('./db.js');
router.get('/login', (req, res) => res.render('login'));
router.get('/register', (req, res) => res.render('register'));
router.post('/login', (req, res) => {
return Promise.try(() => db('users').where({ name: req.body.name }))
.then(users => {
if(users.length == 0) res.render('login', { errors: ['Invalid username or password.'] });
else{
return Promise.try(() => scrypt.verifyHash(req.body.pw, users[0].hash))
.then(() => {
req.session.userID = users[0].id; // Login successful
res.redirect('/');
})
.catch(scrypt.PasswordError, err => res.render('login', { errors: ['Invalid username or password.'] }));
}
});
});
router.post('/register', (req, res) => {
var locals = { errors: [] };
if(req.body.pw !== req.body.confirmPw) locals.errors.push('Passwords do not match.'); // Don't bother checking pw length if they don't match.
else if(req.body.pw.length < 4) locals.errors.push('Password must contain at least 4 characters.');
if(req.body.name.length < 2){ // Don't bother checking for existing user if name is too short.
locals.errors.push('Username must contain at least 2 characters.');
res.render('register', locals);
}
else{ //todo, more errors, for valid chars etc.
return Promise.all(
[
() => scrypt.hash(req.body.pw),
() => db('users').where({ name: req.body.name })
].map(x => Promise.try(x)))
.then(results => {
var hash = results[0];
var users = results[1];
if(users.length > 0){
locals.errors.push('That username is already taken.');
res.render('register', locals);
}
else{ // registration successful
return Promise.try(() => db('users').insert({ name: req.body.name, hash: hash }))
.then(() => db('users').where({ name: req.body.name }))
.then(justInsertedUsers => {
var justInsertedUser = justInsertedUsers[0];
req.session.userID = justInsertedUser.id; // Log in
//todo: temp message: "account created!"
res.redirect('/');
});
}
});
}
});
router.use('/', (req, res, next) => { // Verify login
if(!req.session.userID) res.redirect('/login');
else{
return Promise.try(() => db('users').where({ id: req.session.userID }))
.then(users => {
if(users.length == 0){
req.session.destroy();
res.redirect('/login');
}
else{
req.session.username = users[0].name;
next();
}
});
}
});
router.get('/reconnect', (req, res) => {
if(!connections.getActiveConnection(req.session)) res.redirect('/'); // If there's no active connection, go back to the homepage.
else res.render('reconnect');
});
router.post('/reconnect', (req, res) => {
connections.closeActiveConnection(req.session);
res.redirect('/');
});
router.get('/', (req, res) => {
if(connections.getActiveConnection(req.session)) res.redirect('/reconnect'); // Offer to let them reconnect, if there's an active connection already.
else res.render('index');
});
module.exports = router;