refactor passport so that emails aren't fired if there are errors in save actions

This commit is contained in:
Michael Q Larson
2015-03-31 15:07:34 -07:00
parent 22507de01f
commit a60c5eb92c
3 changed files with 49 additions and 48 deletions

View File

@ -82,6 +82,7 @@ passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, r
User.findOne({ facebook: profile.id }, function(err, existingUser) { User.findOne({ facebook: profile.id }, function(err, existingUser) {
if (existingUser) return done(null, existingUser); if (existingUser) return done(null, existingUser);
User.findOne({ email: profile._json.email }, function(err, existingEmailUser) { User.findOne({ email: profile._json.email }, function(err, existingEmailUser) {
if (err) { return done(err); }
if (existingEmailUser) { if (existingEmailUser) {
req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with Facebook manually from Account Settings.' }); req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with Facebook manually from Account Settings.' });
done(); done();
@ -96,7 +97,6 @@ passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, r
user.profile.location = (profile._json.location) ? profile._json.location.name : ''; user.profile.location = (profile._json.location) ? profile._json.location.name : '';
user.save(function(err) { user.save(function(err) {
done(err, user); done(err, user);
});
var transporter = nodemailer.createTransport({ var transporter = nodemailer.createTransport({
service: 'Mandrill', service: 'Mandrill',
auth: { auth: {
@ -120,6 +120,7 @@ passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, r
transporter.sendMail(mailOptions, function(err) { transporter.sendMail(mailOptions, function(err) {
if (err) { return err; } if (err) { return err; }
}); });
});
} }
}); });
}); });

View File

@ -142,7 +142,7 @@ exports.returnIndividualStory = function(req, res, next) {
try { try {
var votedObj = story.upVotes.filter(function(a){ var votedObj = story.upVotes.filter(function(a){
return a['upVotedByUsername'] === req.user['profile']['username']; return a['upVotedByUsername'] === req.user['profile']['username'];
}) });
if (votedObj.length > 0){ if (votedObj.length > 0){
userVoted = true; userVoted = true;
} }
@ -314,7 +314,7 @@ exports.storySubmission = function(req, res) {
} }
// if duplicate storyLink add unique number // if duplicate storyLink add unique number
storyLink = (storyCount == 0) ? storyLink : storyLink + ' ' + storyCount; storyLink = (storyCount === 0) ? storyLink : storyLink + ' ' + storyCount;
var link = data.link; var link = data.link;
if (link.search(/^https?:\/\//g) === -1) { if (link.search(/^https?:\/\//g) === -1) {

View File

@ -153,11 +153,6 @@ exports.postEmailSignup = function(req, res, next) {
user.save(function(err) { user.save(function(err) {
if (err) { return next(err); } if (err) { return next(err); }
req.logIn(user, function(err) {
if (err) { return next(err); }
res.redirect('/email-signup');
});
});
var transporter = nodemailer.createTransport({ var transporter = nodemailer.createTransport({
service: 'Mandrill', service: 'Mandrill',
auth: { auth: {
@ -175,12 +170,17 @@ exports.postEmailSignup = function(req, res, next) {
'Feel free to email us at this address if you have any questions about Free Code Camp.\n', 'Feel free to email us at this address if you have any questions about Free Code Camp.\n',
"And if you have a moment, check out our blog: blog.freecodecamp.com.\n", "And if you have a moment, check out our blog: blog.freecodecamp.com.\n",
'Good luck with the challenges!\n\n', 'Good luck with the challenges!\n\n',
'- the Volunteer Camp Counselor Team' '- Our All-Volunteer Team'
].join('') ].join('')
}; };
transporter.sendMail(mailOptions, function(err) { transporter.sendMail(mailOptions, function(err) {
if (err) { return err; } if (err) { return err; }
}); });
req.logIn(user, function(err) {
if (err) { return next(err); }
res.redirect('/email-signup');
});
});
}); });
}); });
}; };