start adding error handling to passport
This commit is contained in:
@ -29,6 +29,7 @@ passport.use(new LocalStrategy({ usernameField: 'email' }, function(email, passw
|
|||||||
User.findOne({ email: email }, function(err, user) {
|
User.findOne({ email: email }, function(err, user) {
|
||||||
if (!user) return done(null, false, { message: 'Email ' + email + ' not found'});
|
if (!user) return done(null, false, { message: 'Email ' + email + ' not found'});
|
||||||
user.comparePassword(password, function(err, isMatch) {
|
user.comparePassword(password, function(err, isMatch) {
|
||||||
|
if (err) { return done(err); }
|
||||||
if (isMatch) {
|
if (isMatch) {
|
||||||
return done(null, user);
|
return done(null, user);
|
||||||
} else {
|
} else {
|
||||||
@ -58,6 +59,7 @@ passport.use(new LocalStrategy({ usernameField: 'email' }, function(email, passw
|
|||||||
passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, refreshToken, profile, done) {
|
passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, refreshToken, profile, done) {
|
||||||
if (req.user) {
|
if (req.user) {
|
||||||
User.findOne({ facebook: profile.id }, function(err, existingUser) {
|
User.findOne({ facebook: profile.id }, function(err, existingUser) {
|
||||||
|
if (err) { return done(err); }
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
req.flash('errors', { msg: 'There is already a Facebook account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
|
req.flash('errors', { msg: 'There is already a Facebook account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
|
||||||
done(err);
|
done(err);
|
||||||
@ -77,8 +79,10 @@ passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, r
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
User.findOne({ facebook: profile.id }, function(err, existingUser) {
|
User.findOne({ facebook: profile.id }, function(err, existingUser) {
|
||||||
|
if (err) { return done(err); }
|
||||||
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(err);
|
done(err);
|
||||||
@ -92,30 +96,31 @@ passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, r
|
|||||||
user.profile.picture = 'https://graph.facebook.com/' + profile.id + '/picture?type=large';
|
user.profile.picture = 'https://graph.facebook.com/' + profile.id + '/picture?type=large';
|
||||||
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);
|
if (err) { return done(err); }
|
||||||
});
|
var transporter = nodemailer.createTransport({
|
||||||
var transporter = nodemailer.createTransport({
|
service: 'Mandrill',
|
||||||
service: 'Mandrill',
|
auth: {
|
||||||
auth: {
|
user: secrets.mandrill.user,
|
||||||
user: secrets.mandrill.user,
|
pass: secrets.mandrill.password
|
||||||
pass: secrets.mandrill.password
|
}
|
||||||
}
|
});
|
||||||
});
|
var mailOptions = {
|
||||||
var mailOptions = {
|
to: user.email,
|
||||||
to: user.email,
|
from: 'Team@freecodecamp.com',
|
||||||
from: 'Team@freecodecamp.com',
|
subject: 'Welcome to Free Code Camp!',
|
||||||
subject: 'Welcome to Free Code Camp!',
|
text: [
|
||||||
text: [
|
'Greetings from San Francisco!\n\n',
|
||||||
'Greetings from San Francisco!\n\n',
|
'Thank you for joining our community.\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',
|
||||||
'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'
|
||||||
'- the Volunteer Camp Counselor Team'
|
].join('')
|
||||||
].join('')
|
};
|
||||||
};
|
transporter.sendMail(mailOptions, function(err) {
|
||||||
transporter.sendMail(mailOptions, function(err) {
|
if (err) { return done(err); }
|
||||||
if (err) { return err; }
|
done(null, user);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user