This repository has been archived by the owner on May 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
userrouter.js
169 lines (151 loc) · 4.96 KB
/
userrouter.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
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
166
167
168
/* vim: set ts=2 sw=2 nocin si: */
var userman = require('./userman'),
messages = require('./messages'),
utils = require('./utils');
module.exports = function (app) {
app.get('/login', function (req, res) {
res.render('login', {
locals: {
title: messages.get('Login'),
returnto: req.query.returnto
}
});
});
app.post('/login', function (req, res) {
userman.authenticate({
username: req.body.username,
password: req.body.password
}, function (result) {
if (result.success) {
req.session.userinfo = result.userinfo;
res.redirect(req.param('returnto') || '/');
} else {
req.flash('error', result.error);
res.render('login', {
locals: {
title: messages.get('Login'),
returnto: req.param('returnto'),
userinfo: result.userinfo
}
});
}
});
});
app.get('/logout', function (req, res) {
var redirectURL = req.query.returnto || '/';
req.session.userinfo = null;
res.redirect(redirectURL);
});
app.get('/register', function (req, res) {
res.render('register', { locals: { title: messages.get('Register') } });
});
app.post('/register', function (req, res) {
userman.register(utils.subset(req.body, ["username", "password", "email"]), function (result) {
if (result.success) {
req.session.userinfo = result.userinfo;
req.flash('info', 'register-welcome|' + result.userinfo.username);
res.redirect('/');
} else {
req.flash('error', result.error);
res.render('register', {
locals: {
title: messages.get('Register'),
userinfo: result.userinfo
}
});
}
});
});
app.get('/checkuser', function (req, res) {
userman.checkUser(req.param('name'), function (result) {
res.send(result);
});
});
app.all('/editinfo', function (req, res, next) {
if (req.session.userinfo) next();
else res.redirect('/');
});
app.get('/editinfo', function (req, res) {
// When a user requests to edit his info, we provide him with his current
// info for rendering in the view.
// This won't leak the password, as the view will ask for userinfo.oldpass
// and userinfo.newpass.
res.render('editinfo', {
locals: {
title: messages.get('edit-my-info'),
userinfo: req.session.userinfo
}
});
});
app.post('/editinfo', function (req, res) {
var newInfo = utils.subset(req.body, ['oldpass', 'newpass', 'bio', 'email', 'website', 'birthdate',
'mobile', 'givenname', 'surname', 'address', 'nickname', 'nextNameChangeDate']);
if (req.body.fullname === 'surgiven') newInfo.fullname = newInfo.surname + newInfo.givenname;
else if (req.body.fullname === 'sur-given') newInfo.fullname = newInfo.surname + ' ' + newInfo.givenname;
else newInfo.fullname = newInfo.givenname + ' ' + newInfo.surname;
newInfo.username = req.session.userinfo.username;
userman.editInfo(newInfo, function (result) {
if (result.success) {
// If the editing succeeds, update the user info stored in the session.
req.session.userinfo = result.userinfo;
req.flash('info', 'editinfo-success');
res.redirect('/');
} else {
// On error, give back what was given to us. This fills the password fields.
req.flash('error', result.error);
res.render('editinfo', {
locals: {
title: messages.get('edit-my-info'),
userinfo: newInfo
}
});
}
});
});
app.all('/changename', function (req, res, next) {
// As usual, you have to be logged in to change your username.
if (!req.session.userinfo) res.redirect('/');
else {
// Are you allowed to change it this frequently?
if (req.session.userinfo.nextNameChangeDate > Date.now()) {
// No you're not.
req.flash('error', 'change-name-later|' +
new Date(req.session.userinfo.nextNameChangeDate));
res.redirect('/');
} else {
// Fair enough.
next();
}
}
});
app.get('/changename', function (req, res) {
res.render('changename', {
locals: {
title: messages.get('changing-username'),
nameChange: { oldname: req.session.userinfo.username }
}
});
});
app.post('/changename', function (req, res) {
var nameChange = {
oldname: req.session.userinfo.username,
newname: req.body.newname,
password: req.body.password
};
userman.rename(nameChange, function (result) {
if (result.success) {
req.session.userinfo = result.userinfo;
req.flash('info', 'change-name-success');
res.redirect('/');
} else {
req.flash('error', result.error);
res.render('changename', {
locals: {
title: messages.get('changing-username'),
nameChange: nameChange
}
});
}
});
});
};