Skip to content

Commit

Permalink
refactor passport so that emails aren't fired if there are errors in …
Browse files Browse the repository at this point in the history
…save actions
  • Loading branch information
Michael Q Larson committed Mar 31, 2015
1 parent 22507de commit a60c5eb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 48 deletions.
47 changes: 24 additions & 23 deletions config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, r
User.findOne({ facebook: profile.id }, function(err, existingUser) {
if (existingUser) return done(null, existingUser);
User.findOne({ email: profile._json.email }, function(err, existingEmailUser) {
if (err) { return done(err); }
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.' });
done();
Expand All @@ -96,29 +97,29 @@ passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, r
user.profile.location = (profile._json.location) ? profile._json.location.name : '';
user.save(function(err) {
done(err, user);
});
var transporter = nodemailer.createTransport({
service: 'Mandrill',
auth: {
user: secrets.mandrill.user,
pass: secrets.mandrill.password
}
});
var mailOptions = {
to: user.email,
from: '[email protected]',
subject: 'Welcome to Free Code Camp!',
text: [
'Greetings from San Francisco!\n\n',
'Thank you for joining our community.\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",
'Good luck with the challenges!\n\n',
'- Our All-Volunteer Team'
].join('')
};
transporter.sendMail(mailOptions, function(err) {
if (err) { return err; }
var transporter = nodemailer.createTransport({
service: 'Mandrill',
auth: {
user: secrets.mandrill.user,
pass: secrets.mandrill.password
}
});
var mailOptions = {
to: user.email,
from: '[email protected]',
subject: 'Welcome to Free Code Camp!',
text: [
'Greetings from San Francisco!\n\n',
'Thank you for joining our community.\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",
'Good luck with the challenges!\n\n',
'- Our All-Volunteer Team'
].join('')
};
transporter.sendMail(mailOptions, function(err) {
if (err) { return err; }
});
});
}
});
Expand Down
4 changes: 2 additions & 2 deletions controllers/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ exports.returnIndividualStory = function(req, res, next) {
try {
var votedObj = story.upVotes.filter(function(a){
return a['upVotedByUsername'] === req.user['profile']['username'];
})
});
if (votedObj.length > 0){
userVoted = true;
}
Expand Down Expand Up @@ -314,7 +314,7 @@ exports.storySubmission = function(req, res) {
}

// if duplicate storyLink add unique number
storyLink = (storyCount == 0) ? storyLink : storyLink + ' ' + storyCount;
storyLink = (storyCount === 0) ? storyLink : storyLink + ' ' + storyCount;

var link = data.link;
if (link.search(/^https?:\/\//g) === -1) {
Expand Down
46 changes: 23 additions & 23 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,34 +153,34 @@ exports.postEmailSignup = function(req, res, next) {

user.save(function(err) {
if (err) { return next(err); }
var transporter = nodemailer.createTransport({
service: 'Mandrill',
auth: {
user: secrets.mandrill.user,
pass: secrets.mandrill.password
}
});
var mailOptions = {
to: user.email,
from: '[email protected]',
subject: 'Welcome to Free Code Camp!',
text: [
'Greetings from San Francisco!\n\n',
'Thank you for joining our community.\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",
'Good luck with the challenges!\n\n',
'- Our All-Volunteer Team'
].join('')
};
transporter.sendMail(mailOptions, function(err) {
if (err) { return err; }
});
req.logIn(user, function(err) {
if (err) { return next(err); }
res.redirect('/email-signup');
});
});
var transporter = nodemailer.createTransport({
service: 'Mandrill',
auth: {
user: secrets.mandrill.user,
pass: secrets.mandrill.password
}
});
var mailOptions = {
to: user.email,
from: '[email protected]',
subject: 'Welcome to Free Code Camp!',
text: [
'Greetings from San Francisco!\n\n',
'Thank you for joining our community.\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",
'Good luck with the challenges!\n\n',
'- the Volunteer Camp Counselor Team'
].join('')
};
transporter.sendMail(mailOptions, function(err) {
if (err) { return err; }
});
});
});
};
Expand Down

0 comments on commit a60c5eb

Please sign in to comment.