2014-01-06 19:31:30 -05:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
2014-01-11 22:53:31 -05:00
|
|
|
|
2013-11-30 00:28:30 -05:00
|
|
|
var express = require('express');
|
2014-04-12 12:43:07 -04:00
|
|
|
var cookieParser = require('cookie-parser');
|
|
|
|
var compress = require('compression');
|
|
|
|
var session = require('express-session');
|
|
|
|
var bodyParser = require('body-parser');
|
|
|
|
var logger = require('morgan');
|
|
|
|
var errorHandler = require('errorhandler');
|
2014-04-18 14:29:30 -04:00
|
|
|
var csrf = require('lusca').csrf();
|
2014-04-12 12:43:07 -04:00
|
|
|
var methodOverride = require('method-override');
|
2014-11-04 14:57:00 -08:00
|
|
|
var bodyParser = require('body-parser')
|
2014-04-12 12:43:07 -04:00
|
|
|
|
2014-06-06 15:57:04 -04:00
|
|
|
var _ = require('lodash');
|
2014-04-12 12:43:07 -04:00
|
|
|
var MongoStore = require('connect-mongo')({ session: session });
|
2014-01-28 20:02:45 +01:00
|
|
|
var flash = require('express-flash');
|
2013-12-19 20:17:15 -05:00
|
|
|
var path = require('path');
|
2013-11-30 00:28:30 -05:00
|
|
|
var mongoose = require('mongoose');
|
|
|
|
var passport = require('passport');
|
2014-01-23 22:18:35 -05:00
|
|
|
var expressValidator = require('express-validator');
|
2014-02-21 17:29:06 -05:00
|
|
|
var connectAssets = require('connect-assets');
|
2014-01-23 22:18:35 -05:00
|
|
|
|
2014-01-06 19:31:30 -05:00
|
|
|
/**
|
2014-06-06 14:58:30 -04:00
|
|
|
* Controllers (route handlers).
|
2014-01-06 19:31:30 -05:00
|
|
|
*/
|
2014-01-11 22:53:31 -05:00
|
|
|
|
2014-01-06 19:22:28 -05:00
|
|
|
var homeController = require('./controllers/home');
|
2014-10-20 15:35:42 -07:00
|
|
|
var challengesController = require('./controllers/challenges')
|
2014-01-06 19:22:28 -05:00
|
|
|
var userController = require('./controllers/user');
|
|
|
|
var apiController = require('./controllers/api');
|
|
|
|
var contactController = require('./controllers/contact');
|
2013-11-14 02:29:55 -05:00
|
|
|
|
2014-01-06 19:31:30 -05:00
|
|
|
/**
|
2014-06-06 14:58:30 -04:00
|
|
|
* API keys and Passport configuration.
|
2014-01-06 19:31:30 -05:00
|
|
|
*/
|
2014-01-11 22:53:31 -05:00
|
|
|
|
2013-12-20 01:31:16 -05:00
|
|
|
var secrets = require('./config/secrets');
|
2013-11-26 23:15:13 -05:00
|
|
|
var passportConf = require('./config/passport');
|
|
|
|
|
2014-02-02 05:38:38 -05:00
|
|
|
/**
|
|
|
|
* Create Express server.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var app = express();
|
|
|
|
|
2014-01-11 22:53:31 -05:00
|
|
|
/**
|
2014-06-06 14:58:30 -04:00
|
|
|
* Connect to MongoDB.
|
2014-01-11 22:53:31 -05:00
|
|
|
*/
|
2014-01-13 04:24:31 -05:00
|
|
|
|
2014-02-25 22:39:28 -05:00
|
|
|
mongoose.connect(secrets.db);
|
2014-01-11 22:53:31 -05:00
|
|
|
mongoose.connection.on('error', function() {
|
2014-10-17 19:23:53 -07:00
|
|
|
console.error('MongoDB Connection Error. Make sure MongoDB is running.');
|
2014-01-11 22:53:31 -05:00
|
|
|
});
|
2013-11-13 12:32:22 -05:00
|
|
|
|
2014-01-30 04:18:34 -05:00
|
|
|
var hour = 3600000;
|
2014-04-12 14:17:37 -04:00
|
|
|
var day = hour * 24;
|
|
|
|
var week = day * 7;
|
2014-01-30 04:18:34 -05:00
|
|
|
|
2014-05-06 00:44:30 -04:00
|
|
|
/**
|
2014-06-01 11:52:28 -04:00
|
|
|
* CSRF whitelist.
|
2014-05-06 00:44:30 -04:00
|
|
|
*/
|
|
|
|
|
2014-06-06 15:57:04 -04:00
|
|
|
var csrfExclude = ['/url1', '/url2'];
|
2014-05-06 00:44:30 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Express configuration.
|
|
|
|
*/
|
2014-04-18 14:29:30 -04:00
|
|
|
|
2014-01-11 22:53:31 -05:00
|
|
|
app.set('port', process.env.PORT || 3000);
|
|
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
|
|
app.set('view engine', 'jade');
|
2014-06-06 14:58:30 -04:00
|
|
|
app.use(compress());
|
2014-02-21 17:29:06 -05:00
|
|
|
app.use(connectAssets({
|
2014-10-17 19:23:53 -07:00
|
|
|
paths: [path.join(__dirname, 'public/css'), path.join(__dirname, 'public/js')],
|
|
|
|
helperContext: app.locals
|
2014-02-03 08:34:12 -05:00
|
|
|
}));
|
2014-04-12 12:43:07 -04:00
|
|
|
app.use(logger('dev'));
|
|
|
|
app.use(bodyParser.json());
|
2014-07-02 01:12:11 -04:00
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
2014-01-23 22:18:35 -05:00
|
|
|
app.use(expressValidator());
|
2014-04-12 12:43:07 -04:00
|
|
|
app.use(methodOverride());
|
|
|
|
app.use(cookieParser());
|
|
|
|
app.use(session({
|
2014-10-17 19:23:53 -07:00
|
|
|
resave: true,
|
|
|
|
saveUninitialized: true,
|
|
|
|
secret: secrets.sessionSecret,
|
|
|
|
store: new MongoStore({
|
|
|
|
url: secrets.db,
|
|
|
|
auto_reconnect: true
|
|
|
|
})
|
2014-01-29 00:49:09 -05:00
|
|
|
}));
|
2014-01-11 22:53:31 -05:00
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
2014-06-01 11:52:28 -04:00
|
|
|
app.use(flash());
|
2014-04-18 14:29:30 -04:00
|
|
|
app.use(function(req, res, next) {
|
2014-10-17 19:23:53 -07:00
|
|
|
// CSRF protection.
|
|
|
|
if (_.contains(csrfExclude, req.path)) return next();
|
|
|
|
csrf(req, res, next);
|
2014-04-18 14:29:30 -04:00
|
|
|
});
|
2014-01-11 22:53:31 -05:00
|
|
|
app.use(function(req, res, next) {
|
2014-10-17 19:23:53 -07:00
|
|
|
// Make user object available in templates.
|
|
|
|
res.locals.user = req.user;
|
|
|
|
next();
|
2014-01-11 22:53:31 -05:00
|
|
|
});
|
2014-03-08 14:58:27 -05:00
|
|
|
app.use(function(req, res, next) {
|
2014-10-17 19:23:53 -07:00
|
|
|
// Remember original destination before login.
|
|
|
|
var path = req.path.split('/')[1];
|
|
|
|
if (/auth|login|logout|signup|fonts|favicon/i.test(path)) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
req.session.returnTo = req.path;
|
|
|
|
next();
|
2014-03-08 14:58:27 -05:00
|
|
|
});
|
2014-06-01 11:52:28 -04:00
|
|
|
app.use(express.static(path.join(__dirname, 'public'), { maxAge: week }));
|
2014-01-08 01:37:40 -05:00
|
|
|
|
2014-01-11 22:53:31 -05:00
|
|
|
/**
|
2014-06-06 15:23:28 -04:00
|
|
|
* Main routes.
|
2014-01-11 22:53:31 -05:00
|
|
|
*/
|
2013-11-26 23:22:07 -05:00
|
|
|
|
2014-06-06 15:23:28 -04:00
|
|
|
app.get('/', homeController.index);
|
2014-11-06 16:27:18 -08:00
|
|
|
app.get('/challenges/:challengeNumber', challengesController.returnChallenge)
|
2014-06-06 15:23:28 -04:00
|
|
|
app.get('/login', userController.getLogin);
|
|
|
|
app.post('/login', userController.postLogin);
|
|
|
|
app.get('/logout', userController.logout);
|
|
|
|
app.get('/forgot', userController.getForgot);
|
|
|
|
app.post('/forgot', userController.postForgot);
|
|
|
|
app.get('/reset/:token', userController.getReset);
|
|
|
|
app.post('/reset/:token', userController.postReset);
|
|
|
|
app.get('/signup', userController.getSignup);
|
|
|
|
app.post('/signup', userController.postSignup);
|
2014-10-26 09:32:57 -07:00
|
|
|
app.get('/nonprofits', contactController.getContact);
|
|
|
|
app.post('/nonprofits', contactController.postContact);
|
2014-06-06 15:23:28 -04:00
|
|
|
app.get('/account', passportConf.isAuthenticated, userController.getAccount);
|
|
|
|
app.post('/account/profile', passportConf.isAuthenticated, userController.postUpdateProfile);
|
|
|
|
app.post('/account/password', passportConf.isAuthenticated, userController.postUpdatePassword);
|
|
|
|
app.post('/account/delete', passportConf.isAuthenticated, userController.postDeleteAccount);
|
|
|
|
app.get('/account/unlink/:provider', passportConf.isAuthenticated, userController.getOauthUnlink);
|
2014-11-04 14:57:00 -08:00
|
|
|
app.post('/update-progress', userController.updateProgress);
|
2014-06-06 14:58:30 -04:00
|
|
|
/**
|
|
|
|
* API examples routes.
|
|
|
|
*/
|
|
|
|
|
2014-06-06 15:23:28 -04:00
|
|
|
app.get('/api', apiController.getApi);
|
|
|
|
app.get('/api/lastfm', apiController.getLastfm);
|
|
|
|
app.get('/api/nyt', apiController.getNewYorkTimes);
|
|
|
|
app.get('/api/aviary', apiController.getAviary);
|
|
|
|
app.get('/api/steam', apiController.getSteam);
|
|
|
|
app.get('/api/stripe', apiController.getStripe);
|
|
|
|
app.post('/api/stripe', apiController.postStripe);
|
|
|
|
app.get('/api/scraping', apiController.getScraping);
|
|
|
|
app.get('/api/twilio', apiController.getTwilio);
|
|
|
|
app.post('/api/twilio', apiController.postTwilio);
|
|
|
|
app.get('/api/clockwork', apiController.getClockwork);
|
|
|
|
app.post('/api/clockwork', apiController.postClockwork);
|
|
|
|
app.get('/api/foursquare', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getFoursquare);
|
|
|
|
app.get('/api/tumblr', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getTumblr);
|
|
|
|
app.get('/api/facebook', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getFacebook);
|
|
|
|
app.get('/api/github', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getGithub);
|
|
|
|
app.get('/api/twitter', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getTwitter);
|
|
|
|
app.post('/api/twitter', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.postTwitter);
|
|
|
|
app.get('/api/venmo', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getVenmo);
|
|
|
|
app.post('/api/venmo', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.postVenmo);
|
|
|
|
app.get('/api/linkedin', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getLinkedin);
|
|
|
|
app.get('/api/instagram', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getInstagram);
|
|
|
|
app.get('/api/yahoo', apiController.getYahoo);
|
2014-11-06 17:38:47 -08:00
|
|
|
app.post('/completed_challenge', function(req, res){
|
|
|
|
req.user.challengesCompleted.push(req)
|
|
|
|
console.log("pushed #{req} to user");
|
|
|
|
});
|
2014-02-01 03:30:14 -05:00
|
|
|
|
|
|
|
/**
|
2014-06-06 14:58:30 -04:00
|
|
|
* OAuth sign-in routes.
|
2014-02-01 03:30:14 -05:00
|
|
|
*/
|
|
|
|
|
2014-10-15 14:26:34 -07:00
|
|
|
app.get('/auth/instagram', passport.authenticate('instagram'));
|
2014-11-02 09:07:40 -08:00
|
|
|
app.get('/auth/instagram/callback', passport.authenticate('instagram', { successRedirect: '/challenges/a-one-minute-introduction-to-free-code-camp',failureRedirect: '/login' }), function(req, res) {
|
2014-10-15 14:26:34 -07:00
|
|
|
res.redirect(req.session.returnTo || '/');
|
|
|
|
});
|
|
|
|
app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'user_location'] }));
|
2014-11-02 09:07:40 -08:00
|
|
|
app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/challenges/a-one-minute-introduction-to-free-code-camp',failureRedirect: '/login' }), function(req, res) {
|
2014-10-15 14:26:34 -07:00
|
|
|
res.redirect(req.session.returnTo || '/');
|
|
|
|
});
|
|
|
|
app.get('/auth/github', passport.authenticate('github'));
|
2014-11-02 09:07:40 -08:00
|
|
|
app.get('/auth/github/callback', passport.authenticate('github', { successRedirect: '/challenges/a-one-minute-introduction-to-free-code-camp',failureRedirect: '/login' }), function(req, res) {
|
2014-10-15 14:26:34 -07:00
|
|
|
res.redirect(req.session.returnTo || '/');
|
|
|
|
});
|
|
|
|
app.get('/auth/google', passport.authenticate('google', { scope: 'profile email' }));
|
2014-11-02 09:07:40 -08:00
|
|
|
app.get('/auth/google/callback', passport.authenticate('google', { successRedirect: '/challenges/a-one-minute-introduction-to-free-code-camp',failureRedirect: '/login' }), function(req, res) {
|
2014-10-15 14:26:34 -07:00
|
|
|
res.redirect(req.session.returnTo || '/');
|
|
|
|
});
|
2014-10-17 19:23:53 -07:00
|
|
|
app.get('/auth/twitter', passport.authenticate('twitter'));
|
2014-11-02 09:07:40 -08:00
|
|
|
app.get('/auth/twitter/callback', passport.authenticate('twitter', { successRedirect: '/challenges/a-one-minute-introduction-to-free-code-camp',failureRedirect: '/login' }), function(req, res) {
|
2014-10-17 19:23:53 -07:00
|
|
|
res.redirect(req.session.returnTo || '/');
|
|
|
|
});
|
2014-10-15 14:26:34 -07:00
|
|
|
app.get('/auth/linkedin', passport.authenticate('linkedin', { state: 'SOME STATE' }));
|
2014-11-02 09:07:40 -08:00
|
|
|
app.get('/auth/linkedin/callback', passport.authenticate('linkedin', { successRedirect: '/challenges/a-one-minute-introduction-to-free-code-camp',failureRedirect: '/login' }), function(req, res) {
|
2014-10-15 14:26:34 -07:00
|
|
|
res.redirect(req.session.returnTo || '/');
|
|
|
|
});
|
2014-02-01 03:30:14 -05:00
|
|
|
|
|
|
|
/**
|
2014-06-06 14:58:30 -04:00
|
|
|
* OAuth authorization routes for API examples.
|
2014-02-01 03:30:14 -05:00
|
|
|
*/
|
|
|
|
|
2014-01-11 22:53:31 -05:00
|
|
|
app.get('/auth/foursquare', passport.authorize('foursquare'));
|
2014-01-30 04:18:34 -05:00
|
|
|
app.get('/auth/foursquare/callback', passport.authorize('foursquare', { failureRedirect: '/api' }), function(req, res) {
|
2014-10-17 19:23:53 -07:00
|
|
|
res.redirect('/api/foursquare');
|
2014-01-30 04:18:34 -05:00
|
|
|
});
|
2014-01-11 22:53:31 -05:00
|
|
|
app.get('/auth/tumblr', passport.authorize('tumblr'));
|
2014-01-30 04:18:34 -05:00
|
|
|
app.get('/auth/tumblr/callback', passport.authorize('tumblr', { failureRedirect: '/api' }), function(req, res) {
|
2014-10-17 19:23:53 -07:00
|
|
|
res.redirect('/api/tumblr');
|
2014-01-30 04:18:34 -05:00
|
|
|
});
|
2014-02-10 20:59:39 -05:00
|
|
|
app.get('/auth/venmo', passport.authorize('venmo', { scope: 'make_payments access_profile access_balance access_email access_phone' }));
|
2014-02-10 19:21:54 -05:00
|
|
|
app.get('/auth/venmo/callback', passport.authorize('venmo', { failureRedirect: '/api' }), function(req, res) {
|
2014-10-17 19:23:53 -07:00
|
|
|
res.redirect('/api/venmo');
|
2014-02-10 19:21:54 -05:00
|
|
|
});
|
2013-12-06 22:23:05 -05:00
|
|
|
|
2014-04-18 14:37:06 -04:00
|
|
|
/**
|
|
|
|
* 500 Error Handler.
|
|
|
|
*/
|
|
|
|
|
2014-04-12 12:43:07 -04:00
|
|
|
app.use(errorHandler());
|
|
|
|
|
2014-02-05 19:57:29 -05:00
|
|
|
/**
|
|
|
|
* Start Express server.
|
|
|
|
*/
|
|
|
|
|
2014-01-11 22:53:31 -05:00
|
|
|
app.listen(app.get('port'), function() {
|
2014-10-17 19:23:53 -07:00
|
|
|
console.log('Express server listening on port %d in %s mode', app.get('port'), app.get('env'));
|
2014-01-11 22:53:31 -05:00
|
|
|
});
|
2014-02-25 22:39:28 -05:00
|
|
|
|
2014-11-06 17:38:47 -08:00
|
|
|
module.exports = app;
|
|
|
|
|