Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force emails to be unique with mongo #51

Merged
merged 2 commits into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 26 additions & 38 deletions app/server/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,48 +150,36 @@ UserController.createUser = function(email, password, callback) {
return callback(err);
}

User
.findOneByEmail(email)
.exec(function(err, user){

if (err) {
return callback(err);
}

if (user) {
var u = new User();
u.email = email;
u.password = User.generateHash(password);
u.save(function(err){
if (err){
// Duplicate key error codes
if (err.name === 'MongoError' && (err.code === 11000 || err.code === 11001)) {
return callback({
message: 'An account for this email already exists.'
});
} else {

// Make a new user
var u = new User();
u.email = email;
u.password = User.generateHash(password);
u.save(function(err){
if (err){
return callback(err);
} else {
// yay! success.
var token = u.generateAuthToken();

// Send over a verification email
var verificationToken = u.generateEmailVerificationToken();
Mailer.sendVerificationEmail(email, verificationToken);

return callback(
null,
{
token: token,
user: u
}
);
}

});

}

return callback(err);
} else {
// yay! success.
var token = u.generateAuthToken();

// Send over a verification email
var verificationToken = u.generateEmailVerificationToken();
Mailer.sendVerificationEmail(email, verificationToken);

return callback(
null,
{
token: token,
user: u
}
);
}

});
});
};
Expand Down Expand Up @@ -715,4 +703,4 @@ UserController.getStats = function(callback){
return callback(null, Stats.getUserStats());
};

module.exports = UserController;
module.exports = UserController;
1 change: 1 addition & 0 deletions app/server/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ var schema = new mongoose.Schema({
email: {
type: String,
required: true,
unique: true,
validate: [
validator.isEmail,
'Invalid Email',
Expand Down