Skip to content

Commit

Permalink
general code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sahat committed Jan 10, 2015
1 parent f792c2c commit 82c42e7
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 93 deletions.
64 changes: 38 additions & 26 deletions config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ var TwitterStrategy = require('passport-twitter').Strategy;
var GitHubStrategy = require('passport-github').Strategy;
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var LinkedInStrategy = require('passport-linkedin-oauth2').Strategy;
var OAuthStrategy = require('passport-oauth').OAuthStrategy; // Tumblr
var OAuth2Strategy = require('passport-oauth').OAuth2Strategy; // Venmo, Foursquare
var User = require('../models/User');
var OAuthStrategy = require('passport-oauth').OAuthStrategy;
var OAuth2Strategy = require('passport-oauth').OAuth2Strategy;

var secrets = require('./secrets');
var User = require('../models/User');

passport.serializeUser(function(user, done) {
done(null, user.id);
Expand All @@ -22,8 +23,9 @@ passport.deserializeUser(function(id, done) {
});
});

// Sign in with Instagram.

/**
* Sign in with Instagram.
*/
passport.use(new InstagramStrategy(secrets.instagram,function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
User.findOne({ instagram: profile.id }, function(err, existingUser) {
Expand Down Expand Up @@ -65,8 +67,9 @@ passport.use(new InstagramStrategy(secrets.instagram,function(req, accessToken,
}
}));

// Sign in using Email and Password.

/**
* Sign in using Email and Password.
*/
passport.use(new LocalStrategy({ usernameField: 'email' }, function(email, password, done) {
User.findOne({ email: email }, function(err, user) {
if (!user) return done(null, false, { message: 'Email ' + email + ' not found'});
Expand All @@ -84,7 +87,7 @@ passport.use(new LocalStrategy({ usernameField: 'email' }, function(email, passw
* OAuth Strategy Overview
*
* - User is already logged in.
* - Check if there is an existing account with a <provider> id.
* - Check if there is an existing account with a provider id.
* - If there is, return an error message. (Account merging not supported)
* - Else link new OAuth account with currently logged-in user.
* - User is not logged in.
Expand All @@ -95,8 +98,9 @@ passport.use(new LocalStrategy({ usernameField: 'email' }, function(email, passw
* - Else create a new account.
*/

// Sign in with Facebook.

/**
* Sign in with Facebook.
*/
passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
User.findOne({ facebook: profile.id }, function(err, existingUser) {
Expand Down Expand Up @@ -142,8 +146,9 @@ passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, r
}
}));

// Sign in with GitHub.

/**
* Sign in with GitHub.
*/
passport.use(new GitHubStrategy(secrets.github, function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
User.findOne({ github: profile.id }, function(err, existingUser) {
Expand Down Expand Up @@ -233,8 +238,9 @@ passport.use(new TwitterStrategy(secrets.twitter, function(req, accessToken, tok
}
}));

// Sign in with Google.

/**
* Sign in with Google.
*/
passport.use(new GoogleStrategy(secrets.google, function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
User.findOne({ google: profile.id }, function(err, existingUser) {
Expand Down Expand Up @@ -279,8 +285,9 @@ passport.use(new GoogleStrategy(secrets.google, function(req, accessToken, refre
}
}));

// Sign in with LinkedIn.

/**
* Sign in with LinkedIn.
*/
passport.use(new LinkedInStrategy(secrets.linkedin, function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
User.findOne({ linkedin: profile.id }, function(err, existingUser) {
Expand Down Expand Up @@ -327,8 +334,9 @@ passport.use(new LinkedInStrategy(secrets.linkedin, function(req, accessToken, r
}
}));

// Tumblr API setup.

/**
* Tumblr API OAuth.
*/
passport.use('tumblr', new OAuthStrategy({
requestTokenURL: 'http://www.tumblr.com/oauth/request_token',
accessTokenURL: 'http://www.tumblr.com/oauth/access_token',
Expand All @@ -348,8 +356,9 @@ passport.use('tumblr', new OAuthStrategy({
}
));

// Foursquare API setup.

/**
* Foursquare API OAuth.
*/
passport.use('foursquare', new OAuth2Strategy({
authorizationURL: 'https://foursquare.com/oauth2/authorize',
tokenURL: 'https://foursquare.com/oauth2/access_token',
Expand All @@ -368,8 +377,9 @@ passport.use('foursquare', new OAuth2Strategy({
}
));

// Venmo API setup.

/**
* Venmo API OAuth.
*/
passport.use('venmo', new OAuth2Strategy({
authorizationURL: 'https://api.venmo.com/v1/oauth/authorize',
tokenURL: 'https://api.venmo.com/v1/oauth/access_token',
Expand All @@ -388,15 +398,17 @@ passport.use('venmo', new OAuth2Strategy({
}
));

// Login Required middleware.

/**
* Login Required middleware.
*/
exports.isAuthenticated = function(req, res, next) {
if (req.isAuthenticated()) return next();
res.redirect('/login');
};

// Authorization Required middleware.

/**
* Authorization Required middleware.
*/
exports.isAuthorized = function(req, res, next) {
var provider = req.path.split('/').slice(-1)[0];

Expand Down
23 changes: 0 additions & 23 deletions controllers/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var _ = require('lodash');
* GET /api
* List of API examples.
*/

exports.getApi = function(req, res) {
res.render('api/index', {
title: 'API Examples'
Expand All @@ -34,7 +33,6 @@ exports.getApi = function(req, res) {
* GET /api/foursquare
* Foursquare API example.
*/

exports.getFoursquare = function(req, res, next) {
var token = _.find(req.user.tokens, { kind: 'foursquare' });
async.parallel({
Expand Down Expand Up @@ -69,7 +67,6 @@ exports.getFoursquare = function(req, res, next) {
* GET /api/tumblr
* Tumblr API example.
*/

exports.getTumblr = function(req, res, next) {
var token = _.find(req.user.tokens, { kind: 'tumblr' });
var client = tumblr.createClient({
Expand All @@ -92,7 +89,6 @@ exports.getTumblr = function(req, res, next) {
* GET /api/facebook
* Facebook API example.
*/

exports.getFacebook = function(req, res, next) {
var token = _.find(req.user.tokens, { kind: 'facebook' });
graph.setAccessToken(token.accessToken);
Expand Down Expand Up @@ -122,7 +118,6 @@ exports.getFacebook = function(req, res, next) {
* GET /api/scraping
* Web scraping example using Cheerio library.
*/

exports.getScraping = function(req, res, next) {
request.get('https://news.ycombinator.com/', function(err, request, body) {
if (err) return next(err);
Expand All @@ -142,7 +137,6 @@ exports.getScraping = function(req, res, next) {
* GET /api/github
* GitHub API Example.
*/

exports.getGithub = function(req, res, next) {
var token = _.find(req.user.tokens, { kind: 'github' });
var github = new Github({ token: token.accessToken });
Expand All @@ -161,7 +155,6 @@ exports.getGithub = function(req, res, next) {
* GET /api/aviary
* Aviary image processing example.
*/

exports.getAviary = function(req, res) {
res.render('api/aviary', {
title: 'Aviary API'
Expand All @@ -172,7 +165,6 @@ exports.getAviary = function(req, res) {
* GET /api/nyt
* New York Times API example.
*/

exports.getNewYorkTimes = function(req, res, next) {
var query = querystring.stringify({ 'api-key': secrets.nyt.key, 'list-name': 'young-adult' });
var url = 'http://api.nytimes.com/svc/books/v2/lists?' + query;
Expand All @@ -191,7 +183,6 @@ exports.getNewYorkTimes = function(req, res, next) {
* GET /api/lastfm
* Last.fm API example.
*/

exports.getLastfm = function(req, res, next) {
var lastfm = new LastFmNode(secrets.lastfm);
async.parallel({
Expand Down Expand Up @@ -266,7 +257,6 @@ exports.getLastfm = function(req, res, next) {
* GET /api/twitter
* Twiter API example.
*/

exports.getTwitter = function(req, res, next) {
var token = _.find(req.user.tokens, { kind: 'twitter' });
var T = new Twit({
Expand All @@ -288,7 +278,6 @@ exports.getTwitter = function(req, res, next) {
* POST /api/twitter
* Post a tweet.
*/

exports.postTwitter = function(req, res, next) {
req.assert('tweet', 'Tweet cannot be empty.').notEmpty();
var errors = req.validationErrors();
Expand All @@ -314,7 +303,6 @@ exports.postTwitter = function(req, res, next) {
* GET /api/steam
* Steam API example.
*/

exports.getSteam = function(req, res, next) {
var steamId = '76561197982488301';
var query = { l: 'english', steamid: steamId, key: secrets.steam.apiKey };
Expand Down Expand Up @@ -360,7 +348,6 @@ exports.getSteam = function(req, res, next) {
* GET /api/stripe
* Stripe API example.
*/

exports.getStripe = function(req, res) {
res.render('api/stripe', {
title: 'Stripe API',
Expand All @@ -372,7 +359,6 @@ exports.getStripe = function(req, res) {
* POST /api/stripe
* Make a payment.
*/

exports.postStripe = function(req, res, next) {
var stripeToken = req.body.stripeToken;
var stripeEmail = req.body.stripeEmail;
Expand All @@ -395,7 +381,6 @@ exports.postStripe = function(req, res, next) {
* GET /api/twilio
* Twilio API example.
*/

exports.getTwilio = function(req, res) {
res.render('api/twilio', {
title: 'Twilio API'
Expand All @@ -406,7 +391,6 @@ exports.getTwilio = function(req, res) {
* POST /api/twilio
* Send a text message using Twilio.
*/

exports.postTwilio = function(req, res, next) {
req.assert('number', 'Phone number is required.').notEmpty();
req.assert('message', 'Message cannot be blank.').notEmpty();
Expand All @@ -431,7 +415,6 @@ exports.postTwilio = function(req, res, next) {
* GET /api/clockwork
* Clockwork SMS API example.
*/

exports.getClockwork = function(req, res) {
res.render('api/clockwork', {
title: 'Clockwork SMS API'
Expand All @@ -442,7 +425,6 @@ exports.getClockwork = function(req, res) {
* POST /api/clockwork
* Send a text message using Clockwork SMS
*/

exports.postClockwork = function(req, res, next) {
var message = {
To: req.body.telephone,
Expand All @@ -460,7 +442,6 @@ exports.postClockwork = function(req, res, next) {
* GET /api/venmo
* Venmo API example.
*/

exports.getVenmo = function(req, res, next) {
var token = _.find(req.user.tokens, { kind: 'venmo' });
var query = querystring.stringify({ access_token: token.accessToken });
Expand Down Expand Up @@ -490,7 +471,6 @@ exports.getVenmo = function(req, res, next) {
* POST /api/venmo
* Send money.
*/

exports.postVenmo = function(req, res, next) {
req.assert('user', 'Phone, Email or Venmo User ID cannot be blank').notEmpty();
req.assert('note', 'Please enter a message to accompany the payment').notEmpty();
Expand Down Expand Up @@ -529,7 +509,6 @@ exports.postVenmo = function(req, res, next) {
* GET /api/linkedin
* LinkedIn API example.
*/

exports.getLinkedin = function(req, res, next) {
var token = _.find(req.user.tokens, { kind: 'linkedin' });
var linkedin = Linkedin.init(token.accessToken);
Expand All @@ -546,7 +525,6 @@ exports.getLinkedin = function(req, res, next) {
* GET /api/instagram
* Instagram API example.
*/

exports.getInstagram = function(req, res, next) {
var token = _.find(req.user.tokens, { kind: 'instagram' });
ig.use({ client_id: secrets.instagram.clientID, client_secret: secrets.instagram.clientSecret });
Expand Down Expand Up @@ -588,7 +566,6 @@ exports.getInstagram = function(req, res, next) {
* GET /api/yahoo
* Yahoo API example.
*/

exports.getYahoo = function(req, res) {
Y.YQL('SELECT * FROM weather.forecast WHERE (location = 10007)', function(response) {
var location = response.query.results.channel.location;
Expand Down
7 changes: 1 addition & 6 deletions controllers/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var transporter = nodemailer.createTransport({
* GET /contact
* Contact form page.
*/

exports.getContact = function(req, res) {
res.render('contact', {
title: 'Contact'
Expand All @@ -22,11 +21,7 @@ exports.getContact = function(req, res) {
/**
* POST /contact
* Send a contact form via Nodemailer.
* @param email
* @param name
* @param message
*/

exports.postContact = function(req, res) {
req.assert('name', 'Name cannot be blank').notEmpty();
req.assert('email', 'Email is not valid').isEmail();
Expand Down Expand Up @@ -60,4 +55,4 @@ exports.postContact = function(req, res) {
req.flash('success', { msg: 'Email has been sent successfully!' });
res.redirect('/contact');
});
};
};
3 changes: 1 addition & 2 deletions controllers/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
* GET /
* Home page.
*/

exports.index = function(req, res) {
res.render('home', {
title: 'Home'
});
};
};
Loading

0 comments on commit 82c42e7

Please sign in to comment.