Skip to content

Commit 667fc74

Browse files
committed
refactor(server): simplified validation for email
1 parent 772133d commit 667fc74

File tree

2 files changed

+32
-39
lines changed

2 files changed

+32
-39
lines changed

Diff for: templates/express/controllers/users.js

+11-24
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,12 @@ var mongoose = require('mongoose'),
1010
exports.create = function (req, res, next) {
1111
var newUser = new User(req.body);
1212
newUser.provider = 'local';
13-
1413
newUser.save(function(err) {
15-
if (err) {
16-
// Manually provide our own message for 'unique' validation errors, can't do it from schema
17-
if(err.errors.email.type === 'Value is not unique.') {
18-
err.errors.email.type = 'The specified email address is already in use.';
19-
}
20-
return res.json(400, err);
21-
}
22-
14+
if (err) return next(err);
15+
2316
req.logIn(newUser, function(err) {
2417
if (err) return next(err);
25-
18+
2619
return res.json(req.user.userInfo);
2720
});
2821
});
@@ -35,13 +28,10 @@ exports.show = function (req, res, next) {
3528
var userId = req.params.id;
3629

3730
User.findById(userId, function (err, user) {
38-
if (err) return next(new Error('Failed to load User'));
39-
40-
if (user) {
41-
res.send({ profile: user.profile });
42-
} else {
43-
res.send(404, 'USER_NOT_FOUND');
44-
}
31+
if (err) return next(err);
32+
if (!user) return res.send(404);
33+
34+
res.send({ profile: user.profile });
4535
});
4636
};
4737

@@ -55,17 +45,14 @@ exports.changePassword = function(req, res, next) {
5545

5646
User.findById(userId, function (err, user) {
5747
if(user.authenticate(oldPass)) {
58-
5948
user.password = newPass;
6049
user.save(function(err) {
61-
if (err) {
62-
res.send(500, err);
63-
} else {
64-
res.send(200);
65-
}
50+
if (err) return res.send(400);
51+
52+
res.send(200);
6653
});
6754
} else {
68-
res.send(400);
55+
res.send(403);
6956
}
7057
});
7158
};

Diff for: templates/express/models/user.js

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
'use strict';
22

33
var mongoose = require('mongoose'),
4-
uniqueValidator = require('mongoose-unique-validator'),
54
Schema = mongoose.Schema,
65
crypto = require('crypto');
76

8-
var authTypes = ['github', 'twitter', 'facebook', 'google'],
9-
SALT_WORK_FACTOR = 10;
7+
var authTypes = ['github', 'twitter', 'facebook', 'google'];
108

119
/**
1210
* User Schema
1311
*/
1412
var UserSchema = new Schema({
1513
name: String,
16-
email: {
17-
type: String,
18-
unique: true
19-
},
14+
email: String,
2015
role: {
2116
type: String,
2217
default: 'user'
@@ -68,9 +63,6 @@ UserSchema
6863
/**
6964
* Validations
7065
*/
71-
var validatePresenceOf = function(value) {
72-
return value && value.length;
73-
};
7466

7567
// Validate empty email
7668
UserSchema
@@ -90,10 +82,24 @@ UserSchema
9082
return hashedPassword.length;
9183
}, 'Password cannot be blank');
9284

93-
/**
94-
* Plugins
95-
*/
96-
UserSchema.plugin(uniqueValidator, { message: 'Value is not unique.' });
85+
// Validate email is not taken
86+
UserSchema
87+
.path('email')
88+
.validate(function(value, respond) {
89+
var self = this;
90+
this.constructor.findOne({email: value}, function(err, user) {
91+
if(err) throw err;
92+
if(user) {
93+
if(self.id === user.id) return respond(true);
94+
return respond(false);
95+
}
96+
respond(true);
97+
});
98+
}, 'The specified email address is already in use.');
99+
100+
var validatePresenceOf = function(value) {
101+
return value && value.length;
102+
};
97103

98104
/**
99105
* Pre-save hook
@@ -147,4 +153,4 @@ UserSchema.methods = {
147153
}
148154
};
149155

150-
mongoose.model('User', UserSchema);
156+
module.exports = mongoose.model('User', UserSchema);

0 commit comments

Comments
 (0)