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 1 commit
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
61 changes: 24 additions & 37 deletions app/server/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,48 +150,35 @@ 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){
if (err.name === 'MongoError' && (err.code === 11000 || err.code === 11001)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment here about what the hard-coded error codes mean?

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
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