Skip to content

Commit c3e8250

Browse files
committed
Put in function to handle determining the user scheme
1 parent a346f3d commit c3e8250

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

user-routes.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,40 @@ function createToken(user) {
1616
return jwt.sign(_.omit(user, 'password'), config.secret, { expiresInMinutes: 60*5 });
1717
}
1818

19-
app.post('/users', function(req, res) {
20-
var username;
21-
var userScheme;
19+
function getUserScheme(req) {
2220

21+
var username;
22+
var type;
23+
2324
// The POST contains a username and not an email
2425
if(req.body.username) {
2526
username = req.body.username;
26-
userScheme = 'username';
27+
type = 'username';
2728
}
2829
// The POST contains an email and not an username
2930
else if(req.body.email) {
3031
username = req.body.email;
31-
userScheme = 'email';
32+
type = 'email';
3233
}
3334

35+
return {
36+
username: username,
37+
type: type
38+
}
39+
}
40+
41+
app.post('/users', function(req, res) {
42+
43+
userScheme = getUserScheme(req);
44+
3445
if (!req.body.username && !req.body.email || !req.body.password) {
3546
return res.status(400).send("You must send the username and the password");
3647
}
37-
if (_.find(users, {username: username}) || _.find(users, {email: username})) {
48+
if (_.find(users, {username: userScheme.username}) || _.find(users, {email: userScheme.username})) {
3849
return res.status(400).send("A user with that username already exists");
3950
}
4051

41-
var profile = _.pick(req.body, userScheme, 'password', 'extra');
52+
var profile = _.pick(req.body, userScheme.type, 'password', 'extra');
4253
profile.id = _.max(users, 'id').id + 1;
4354

4455
users.push(profile);
@@ -50,25 +61,13 @@ app.post('/users', function(req, res) {
5061

5162
app.post('/sessions/create', function(req, res) {
5263

53-
var username;
54-
var userScheme;
55-
56-
// The POST contains a username and not an email
57-
if(req.body.username) {
58-
username = req.body.username;
59-
userScheme = 'username';
60-
}
61-
// The POST contains an email and not an username
62-
else if(req.body.email) {
63-
username = req.body.email;
64-
userScheme = 'email';
65-
}
64+
userScheme = getUserScheme(req);
6665

6766
if (!req.body.username && !req.body.email || !req.body.password) {
6867
return res.status(400).send("You must send the username and the password");
6968
}
7069

71-
var user = _.find(users, {username: username}) || _.find(users, {email: username});
70+
var user = _.find(users, {username: userScheme.username}) || _.find(users, {email: userScheme.username});
7271
if (!user) {
7372
return res.status(401).send({message:"The username or password don't match", user: user});
7473
}
@@ -80,4 +79,4 @@ app.post('/sessions/create', function(req, res) {
8079
res.status(201).send({
8180
id_token: createToken(user)
8281
});
83-
});
82+
});

0 commit comments

Comments
 (0)