Skip to content

Commit

Permalink
refactor(server): simplified validation for email
Browse files Browse the repository at this point in the history
  • Loading branch information
DaftMonk committed Feb 8, 2014
1 parent 772133d commit 667fc74
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 39 deletions.
35 changes: 11 additions & 24 deletions templates/express/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,12 @@ var mongoose = require('mongoose'),
exports.create = function (req, res, next) {
var newUser = new User(req.body);
newUser.provider = 'local';

newUser.save(function(err) {
if (err) {
// Manually provide our own message for 'unique' validation errors, can't do it from schema
if(err.errors.email.type === 'Value is not unique.') {
err.errors.email.type = 'The specified email address is already in use.';
}
return res.json(400, err);
}

if (err) return next(err);

req.logIn(newUser, function(err) {
if (err) return next(err);

return res.json(req.user.userInfo);
});
});
Expand All @@ -35,13 +28,10 @@ exports.show = function (req, res, next) {
var userId = req.params.id;

User.findById(userId, function (err, user) {
if (err) return next(new Error('Failed to load User'));

if (user) {
res.send({ profile: user.profile });
} else {
res.send(404, 'USER_NOT_FOUND');
}
if (err) return next(err);
if (!user) return res.send(404);

res.send({ profile: user.profile });
});
};

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

User.findById(userId, function (err, user) {
if(user.authenticate(oldPass)) {

user.password = newPass;
user.save(function(err) {
if (err) {
res.send(500, err);
} else {
res.send(200);
}
if (err) return res.send(400);

res.send(200);
});
} else {
res.send(400);
res.send(403);
}
});
};
Expand Down
36 changes: 21 additions & 15 deletions templates/express/models/user.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
'use strict';

var mongoose = require('mongoose'),
uniqueValidator = require('mongoose-unique-validator'),
Schema = mongoose.Schema,
crypto = require('crypto');

var authTypes = ['github', 'twitter', 'facebook', 'google'],
SALT_WORK_FACTOR = 10;
var authTypes = ['github', 'twitter', 'facebook', 'google'];

/**
* User Schema
*/
var UserSchema = new Schema({
name: String,
email: {
type: String,
unique: true
},
email: String,
role: {
type: String,
default: 'user'
Expand Down Expand Up @@ -68,9 +63,6 @@ UserSchema
/**
* Validations
*/
var validatePresenceOf = function(value) {
return value && value.length;
};

// Validate empty email
UserSchema
Expand All @@ -90,10 +82,24 @@ UserSchema
return hashedPassword.length;
}, 'Password cannot be blank');

/**
* Plugins
*/
UserSchema.plugin(uniqueValidator, { message: 'Value is not unique.' });
// Validate email is not taken
UserSchema
.path('email')
.validate(function(value, respond) {
var self = this;
this.constructor.findOne({email: value}, function(err, user) {
if(err) throw err;
if(user) {
if(self.id === user.id) return respond(true);
return respond(false);
}
respond(true);
});
}, 'The specified email address is already in use.');

var validatePresenceOf = function(value) {
return value && value.length;
};

/**
* Pre-save hook
Expand Down Expand Up @@ -147,4 +153,4 @@ UserSchema.methods = {
}
};

mongoose.model('User', UserSchema);
module.exports = mongoose.model('User', UserSchema);

0 comments on commit 667fc74

Please sign in to comment.