diff --git a/app.js b/app.js index 7f15966228..2710ca1656 100644 --- a/app.js +++ b/app.js @@ -2,11 +2,20 @@ if (process.env.NODE_ENV !== 'development') { require('newrelic'); } require('dotenv').load(); -/** - * Module dependencies. - */ +// handle uncaught exceptions. Forever will restart process on shutdown +process.on('uncaughtException', function (err) { + console.error( + (new Date()).toUTCString() + ' uncaughtException:', + err.message + ); + console.error(err.stack); + /* eslint-disable no-process-exit */ + process.exit(1); + /* eslint-enable no-process-exit */ +}); var express = require('express'), + accepts = require('accepts'), cookieParser = require('cookie-parser'), compress = require('compression'), session = require('express-session'), @@ -430,7 +439,6 @@ app.get('/account/unlink/:provider', userController.getOauthUnlink); app.get('/sitemap.xml', resourcesController.sitemap); - /** * OAuth sign-in routes. */ @@ -492,17 +500,54 @@ app.get( } ); -//put this route last +app.get('/induce-vomiting', function(req, res, next) { + next(new Error('vomiting induced')); +}); + +// put this route last app.get( '/:username', userController.returnUser ); - /** * 500 Error Handler. */ -app.use(errorHandler()); +if (process.env.NODE_ENV === 'development') { + app.use(errorHandler({ log: true })); +} else { + // error handling in production + app.use(function(err, req, res, next) { + + // respect err.status + if (err.status) { + res.statusCode = err.status; + } + + // default status code to 500 + if (res.statusCode < 400) { + res.statusCode = 500; + } + + // parse res type + var accept = accepts(req); + var type = accept.type('html', 'json', 'text'); + + var message = 'opps! Something went wrong. Please try again later'; + if (type === 'html') { + req.flash('errors', { msg: message }); + return res.redirect('/'); + // json + } else if (type === 'json') { + res.setHeader('Content-Type', 'application/json'); + return res.send({ message: message }); + // plain text + } else { + res.setHeader('Content-Type', 'text/plain'); + return res.send(message); + } + }); +} /** * Start Express server. diff --git a/config/passport.js b/config/passport.js index af46f12713..970753ce21 100644 --- a/config/passport.js +++ b/config/passport.js @@ -27,6 +27,7 @@ passport.deserializeUser(function(id, done) { passport.use(new LocalStrategy({ usernameField: 'email' }, function(email, password, done) { User.findOne({ email: email }, function(err, user) { + if (err) { return done(err); } if (!user) return done(null, false, { message: 'Email ' + email + ' not found'}); user.comparePassword(password, function(err, isMatch) { if (isMatch) { @@ -60,17 +61,19 @@ passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, r User.findOne({ facebook: profile.id }, function(err, 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.' }); - done(err); + done(); } else { User.findById(req.user.id, function(err, user) { + if (err) { return done(err); } user.facebook = profile.id; user.tokens.push({ kind: 'facebook', accessToken: accessToken }); user.profile.name = user.profile.name || profile.displayName; user.profile.gender = user.profile.gender || profile._json.gender; user.profile.picture = user.profile.picture || 'https://s3.amazonaws.com/freecodecamp/camper-image-placeholder.png'; user.save(function(err) { + if (err) { return done(err); } req.flash('info', { msg: 'Facebook account has been linked.' }); - done(err, user); + done(null, user); }); }); } @@ -81,7 +84,7 @@ passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, r User.findOne({ email: profile._json.email }, function(err, 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.' }); - done(err); + done(); } else { var user = new User(); user.email = profile._json.email; @@ -128,9 +131,10 @@ passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, r passport.use(new GitHubStrategy(secrets.github, function(req, accessToken, refreshToken, profile, done) { if (req.user) { User.findOne({ github: profile.id }, function(err, existingUser) { + if (err) { return done(err); } if (existingUser) { req.flash('errors', { msg: 'There is already a GitHub account that belongs to you. Sign in with that account or delete it, then link it with your current account.' }); - done(err); + done(); } else { User.findById(req.user.id, function(err, user) { user.github = profile.id; @@ -140,19 +144,22 @@ passport.use(new GitHubStrategy(secrets.github, function(req, accessToken, refre user.profile.location = user.profile.location || profile._json.location; user.profile.website = user.profile.website || profile._json.blog; user.save(function(err) { + if (err) { return done(err); } req.flash('info', { msg: 'GitHub account has been linked.' }); - done(err, user); + done(null, user); }); }); } }); } else { User.findOne({ github: profile.id }, function(err, existingUser) { + if (err) { return done(err); } 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 GitHub manually from Account Settings.' }); - done(err); + done(null); } else { var user = new User(); user.email = profile._json.email; @@ -163,30 +170,31 @@ passport.use(new GitHubStrategy(secrets.github, function(req, accessToken, refre user.profile.location = profile._json.location; user.profile.website = profile._json.blog; 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: 'Team@freecodecamp.com', - 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; } + if (err) { return done(err); } + var transporter = nodemailer.createTransport({ + service: 'Mandrill', + auth: { + user: secrets.mandrill.user, + pass: secrets.mandrill.password + } + }); + var mailOptions = { + to: user.email, + from: 'Team@freecodecamp.com', + 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 done(err); } + done(null, user); + }); }); } }); @@ -199,9 +207,10 @@ passport.use(new GitHubStrategy(secrets.github, function(req, accessToken, refre passport.use(new TwitterStrategy(secrets.twitter, function(req, accessToken, tokenSecret, profile, done) { if (req.user) { User.findOne({ twitter: profile.id }, function(err, existingUser) { + if (err) { return done(err); } if (existingUser) { req.flash('errors', { msg: 'There is already a Twitter account that belongs to you. Sign in with that account or delete it, then link it with your current account.' }); - done(err); + done(); } else { User.findById(req.user.id, function(err, user) { user.twitter = profile.id; @@ -212,8 +221,9 @@ passport.use(new TwitterStrategy(secrets.twitter, function(req, accessToken, tok user.profile.picture = user.profile.picture || profile._json.profile_image_url_https.replace('_normal', ''); user.profile.twitterHandle = user.profile.twitterHandle || profile.username.toLowerCase(); user.save(function(err) { + if (err) { return done(err); } req.flash('info', { msg: 'Twitter account has been linked.' }); - done(err, user); + done(null, user); }); }); } @@ -221,6 +231,7 @@ passport.use(new TwitterStrategy(secrets.twitter, function(req, accessToken, tok } else { User.findOne({ twitter: profile.id }, function(err, existingUser) { + if (err) { return done(err); } if (existingUser) return done(null, existingUser); var user = new User(); user.profile.username = profile.username.toLowerCase(); @@ -231,7 +242,8 @@ passport.use(new TwitterStrategy(secrets.twitter, function(req, accessToken, tok user.profile.picture = profile._json.profile_image_url_https.replace('_normal', ''); user.profile.twitterHandle = user.profile.twitterHandle || profile.username.toLowerCase(); user.save(function(err) { - done(err, user); + if (err) { return done(err); } + done(null, user); }); }); } @@ -242,30 +254,35 @@ passport.use(new TwitterStrategy(secrets.twitter, function(req, accessToken, tok passport.use(new GoogleStrategy(secrets.google, function(req, accessToken, refreshToken, profile, done) { if (req.user) { User.findOne({ google: profile.id }, function(err, existingUser) { + if (err) { return done(err); } if (existingUser) { req.flash('errors', { msg: 'There is already a Google account that belongs to you. Sign in with that account or delete it, then link it with your current account.' }); - done(err); + done(); } else { User.findById(req.user.id, function(err, user) { + if (err) { return done(err); } user.google = profile.id; user.tokens.push({ kind: 'google', accessToken: accessToken }); user.profile.name = user.profile.name || profile.displayName; user.profile.gender = user.profile.gender || profile._json.gender; user.profile.picture = user.profile.picture || profile._json.picture; user.save(function(err) { + if (err) { return done(err); } req.flash('info', { msg: 'Google account has been linked.' }); - done(err, user); + done(null, user); }); }); } }); } else { User.findOne({ google: profile.id }, function(err, existingUser) { + if (err) { return done(err); } 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 Google manually from Account Settings.' }); - done(err); + done(); } else { var user = new User(); user.email = profile._json.email; @@ -275,30 +292,31 @@ passport.use(new GoogleStrategy(secrets.google, function(req, accessToken, refre user.profile.gender = profile._json.gender; user.profile.picture = profile._json.picture; 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: 'Team@freecodecamp.com', - 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; } + if (err) { return done(err); } + var transporter = nodemailer.createTransport({ + service: 'Mandrill', + auth: { + user: secrets.mandrill.user, + pass: secrets.mandrill.password + } + }); + var mailOptions = { + to: user.email, + from: 'Team@freecodecamp.com', + 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; } + done(null, user); + }); }); } }); @@ -311,11 +329,13 @@ passport.use(new GoogleStrategy(secrets.google, function(req, accessToken, refre passport.use(new LinkedInStrategy(secrets.linkedin, function(req, accessToken, refreshToken, profile, done) { if (req.user) { User.findOne({ linkedin: profile.id }, function(err, existingUser) { + if (err) { return done(err); } if (existingUser) { req.flash('errors', { msg: 'There is already a LinkedIn account that belongs to you. Sign in with that account or delete it, then link it with your current account.' }); - done(err); + done(); } else { User.findById(req.user.id, function(err, user) { + if (err) { return done(err); } user.linkedin = profile.id; user.tokens.push({ kind: 'linkedin', accessToken: accessToken }); user.profile.name = user.profile.name || profile.displayName; @@ -323,8 +343,9 @@ passport.use(new LinkedInStrategy(secrets.linkedin, function(req, accessToken, r user.profile.picture = user.profile.picture || profile._json.pictureUrl; user.profile.website = user.profile.website || profile._json.publicProfileUrl; user.save(function(err) { + if (err) { return done(err); } req.flash('info', { msg: 'LinkedIn account has been linked.' }); - done(err, user); + done(null, user); }); }); } @@ -333,9 +354,10 @@ passport.use(new LinkedInStrategy(secrets.linkedin, function(req, accessToken, r User.findOne({ linkedin: profile.id }, function(err, existingUser) { if (existingUser) return done(null, existingUser); User.findOne({ email: profile._json.emailAddress }, 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 LinkedIn manually from Account Settings.' }); - done(err); + done(); } else { var user = new User(); user.linkedin = profile.id; @@ -346,30 +368,31 @@ passport.use(new LinkedInStrategy(secrets.linkedin, function(req, accessToken, r user.profile.picture = profile._json.pictureUrl; user.profile.website = profile._json.publicProfileUrl; 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: 'Team@freecodecamp.com', - 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; } + if (err) { return done(err); } + var transporter = nodemailer.createTransport({ + service: 'Mandrill', + auth: { + user: secrets.mandrill.user, + pass: secrets.mandrill.password + } + }); + var mailOptions = { + to: user.email, + from: 'Team@freecodecamp.com', + 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; } + done(null, user); + }); }); } }); @@ -380,7 +403,7 @@ passport.use(new LinkedInStrategy(secrets.linkedin, function(req, accessToken, r // Login Required middleware. exports.isAuthenticated = function(req, res, next) { - if (req.isAuthenticated()) return next(); + if (req.isAuthenticated()) { return next(); } res.redirect('/login'); }; @@ -394,4 +417,4 @@ exports.isAuthorized = function(req, res, next) { } else { res.redirect('/auth/' + provider); } -}; \ No newline at end of file +}; diff --git a/package.json b/package.json index 732eb86787..ffe8c6281b 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,11 @@ "postinstall": "node seed_data/seed.js" }, "dependencies": { + "accepts": "^1.2.5", "async": "^0.9.0", "bcrypt-nodejs": "^0.0.3", "body-parser": "^1.9.3", + "chai-jquery": "^2.0.0", "cheerio": "^0.18.0", "clockwork": "^0.1.1", "compression": "^1.2.1", diff --git a/public/js/lib/bonfire/plugin_v0.1.4.js b/public/js/lib/bonfire/plugin_v0.1.4.js index 9aa5978c06..f71c0d34f1 100644 --- a/public/js/lib/bonfire/plugin_v0.1.4.js +++ b/public/js/lib/bonfire/plugin_v0.1.4.js @@ -17,6 +17,7 @@ var run = function(code) { } application.remote.output(result); + self.close(); }; diff --git a/seed_data/bonfires.json b/seed_data/bonfires.json index c298c97db7..a686f7e7f7 100644 --- a/seed_data/bonfires.json +++ b/seed_data/bonfires.json @@ -176,7 +176,7 @@ "tests": [ "assert.deepEqual(chunk(['a', 'b', 'c', 'd'], 2), [['a', 'b'], ['c', 'd']], 'should return chunked arrays');", "assert.deepEqual(chunk([0, 1, 2, 3, 4, 5], 3), [[0, 1, 2], [3, 4, 5]], 'should return chunked arrays');", - "assert.deepEqual(chunk([0, 1, 2, 3, 4, 5], 4), [[0, 1, 2, 3], [4, 5]], 'should return cthe last chunk as remaining elements');" + "assert.deepEqual(chunk([0, 1, 2, 3, 4, 5], 4), [[0, 1, 2, 3], [4, 5]], 'should return the last chunk as remaining elements');" ] }, {