Refactoring
This commit is contained in:
6
app.js
6
app.js
@ -61,11 +61,11 @@ app.post('/account/settings', passportConf.isAuthenticated, user.postAccountSett
|
|||||||
app.get('/partials/:name', home.partials);
|
app.get('/partials/:name', home.partials);
|
||||||
|
|
||||||
app.get('/api', api.getApi);
|
app.get('/api', api.getApi);
|
||||||
app.get('/api/foursquare', passportConf.isAuthenticated, passportConf.isAuthorized('foursquare'), api.getFoursquare);
|
app.get('/api/foursquare', passportConf.isAuthenticated, api.getFoursquare);
|
||||||
app.get('/api/tumblr', passportConf.isAuthenticated, passportConf.isAuthorized('foursquare'), api.getTumblr);
|
app.get('/api/tumblr', passportConf.isAuthenticated, api.getTumblr);
|
||||||
app.get('/api/facebook', passportConf.isAuthenticated, api.getFacebook);
|
app.get('/api/facebook', passportConf.isAuthenticated, api.getFacebook);
|
||||||
app.get('/api/scraping', api.getScraping);
|
app.get('/api/scraping', api.getScraping);
|
||||||
app.get('/api/github', passportConf.isAuthorized('github'), api.getGithub);
|
app.get('/api/github', api.getGithub);
|
||||||
app.get('/api/lastfm', api.getLastfm);
|
app.get('/api/lastfm', api.getLastfm);
|
||||||
app.get('/api/nyt', api.getNewYorkTimes);
|
app.get('/api/nyt', api.getNewYorkTimes);
|
||||||
app.get('/api/twilio', api.getTwilio);
|
app.get('/api/twilio', api.getTwilio);
|
||||||
|
@ -138,15 +138,3 @@ exports.isAuthenticated = function(req, res, next) {
|
|||||||
if (req.isAuthenticated()) return next();
|
if (req.isAuthenticated()) return next();
|
||||||
res.redirect('/login');
|
res.redirect('/login');
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.isAuthorized = function(provider) {
|
|
||||||
return function(req, res, next) {
|
|
||||||
var accessToken = _.findWhere(req.user.tokens, { kind: provider });
|
|
||||||
if (accessToken) return next();
|
|
||||||
res.render('api/unauthorized', {
|
|
||||||
title: provider + ' API',
|
|
||||||
provider: provider,
|
|
||||||
user: req.user
|
|
||||||
});
|
|
||||||
};
|
|
||||||
};
|
|
@ -24,30 +24,24 @@ exports.getApi = function(req, res) {
|
|||||||
* GET /api/foursquare
|
* GET /api/foursquare
|
||||||
*/
|
*/
|
||||||
exports.getFoursquare = function(req, res) {
|
exports.getFoursquare = function(req, res) {
|
||||||
var token = _.findWhere(req.user.tokens, { kind: 'foursquare' });
|
var foursquareToken = _.findWhere(req.user.tokens, { kind: 'foursquare' });
|
||||||
if (!token) {
|
if (!foursquareToken) return res.redirect('/auth/foursquare');
|
||||||
return res.render('api/unauthorized', {
|
|
||||||
title: 'Foursquare API',
|
|
||||||
provider: 'Foursquare',
|
|
||||||
user: req.user
|
|
||||||
});
|
|
||||||
}
|
|
||||||
async.parallel({
|
async.parallel({
|
||||||
trendingVenues: function(callback) {
|
trendingVenues: function(callback) {
|
||||||
var geo = geoip.lookup('4.17.136.0');
|
var geo = geoip.lookup('4.17.136.0');
|
||||||
var lat = geo.ll[0];
|
var lat = geo.ll[0];
|
||||||
var lon = geo.ll[1];
|
var lon = geo.ll[1];
|
||||||
foursquare.Venues.getTrending(lat, lon, { limit: 50 }, token.token, function(err, results) {
|
foursquare.Venues.getTrending(lat, lon, { limit: 50 }, foursquareToken.token, function(err, results) {
|
||||||
callback(err, results);
|
callback(err, results);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
venueDetail: function(callback) {
|
venueDetail: function(callback) {
|
||||||
foursquare.Venues.getVenue('49da74aef964a5208b5e1fe3', token.token, function(err, results) {
|
foursquare.Venues.getVenue('49da74aef964a5208b5e1fe3', foursquareToken.token, function(err, results) {
|
||||||
callback(err, results);
|
callback(err, results);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
userCheckins: function(callback) {
|
userCheckins: function(callback) {
|
||||||
foursquare.Users.getCheckins('self', null, token.token, function(err, results) {
|
foursquare.Users.getCheckins('self', null, foursquareToken.token, function(err, results) {
|
||||||
callback(err, results);
|
callback(err, results);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -67,23 +61,14 @@ exports.getFoursquare = function(req, res) {
|
|||||||
* GET /api/tumblr
|
* GET /api/tumblr
|
||||||
*/
|
*/
|
||||||
exports.getTumblr = function(req, res) {
|
exports.getTumblr = function(req, res) {
|
||||||
var token = _.findWhere(req.user.tokens, { kind: 'tumblr' });
|
var tumblrToken = _.findWhere(req.user.tokens, { kind: 'tumblr' });
|
||||||
// TODO: MIDDLEWARE
|
if (!tumblrToken) return res.redirect('/auth/tumblr');
|
||||||
if (!token) {
|
|
||||||
return res.render('api/unauthorized', {
|
|
||||||
title: 'Tumblr API',
|
|
||||||
provider: 'Tumblr',
|
|
||||||
user: req.user
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
var client = tumblr.createClient({
|
var client = tumblr.createClient({
|
||||||
consumer_key: config.tumblr.consumerKey,
|
consumer_key: config.tumblr.consumerKey,
|
||||||
consumer_secret: config.tumblr.consumerSecret,
|
consumer_secret: config.tumblr.consumerSecret,
|
||||||
token: token.token,
|
token: tumblrToken.token,
|
||||||
token_secret: token.tokenSecret
|
token_secret: tumblrToken.tokenSecret
|
||||||
});
|
});
|
||||||
|
|
||||||
client.posts('goddess-of-imaginary-light.tumblr.com', { type: 'photo' }, function(err, data) {
|
client.posts('goddess-of-imaginary-light.tumblr.com', { type: 'photo' }, function(err, data) {
|
||||||
res.render('api/tumblr', {
|
res.render('api/tumblr', {
|
||||||
title: 'Tumblr API',
|
title: 'Tumblr API',
|
||||||
@ -98,17 +83,9 @@ exports.getTumblr = function(req, res) {
|
|||||||
* GET /api/facebook
|
* GET /api/facebook
|
||||||
*/
|
*/
|
||||||
exports.getFacebook = function(req, res) {
|
exports.getFacebook = function(req, res) {
|
||||||
var token = _.findWhere(req.user.tokens, { kind: 'facebook' });
|
var facebookToken = _.findWhere(req.user.tokens, { kind: 'facebook' });
|
||||||
// TODO: MIDDLEWARE
|
if (!facebookToken) return res.redirect('/auth/facebook');
|
||||||
// TODO: OR just redirect directly to /auth/facebook
|
graph.setAccessToken(facebookToken.token);
|
||||||
if (!token) {
|
|
||||||
return res.render('api/unauthorized', {
|
|
||||||
title: 'Facebook API',
|
|
||||||
provider: 'Facebook',
|
|
||||||
user: req.user
|
|
||||||
});
|
|
||||||
}
|
|
||||||
graph.setAccessToken(token.token);
|
|
||||||
async.parallel({
|
async.parallel({
|
||||||
getMe: function(done) {
|
getMe: function(done) {
|
||||||
graph.get(req.user.facebook, function(err, me) {
|
graph.get(req.user.facebook, function(err, me) {
|
||||||
@ -129,9 +106,6 @@ exports.getFacebook = function(req, res) {
|
|||||||
user: req.user
|
user: req.user
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getScraping = function(req, res) {
|
exports.getScraping = function(req, res) {
|
||||||
@ -150,16 +124,10 @@ exports.getScraping = function(req, res) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.getGithub = function(req, res) {
|
exports.getGithub = function(req, res) {
|
||||||
var token = _.findWhere(req.user.tokens, { kind: 'github' });
|
var githubToken = _.findWhere(req.user.tokens, { kind: 'github' });
|
||||||
if (!token) {
|
if (!githubToken) return res.redirect('/auth/github');
|
||||||
return res.render('api/unauthorized', {
|
|
||||||
title: 'GitHub API',
|
|
||||||
provider: 'GitHub',
|
|
||||||
user: req.user
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// TODO: Fix rate limit on passport-github token
|
// TODO: Fix rate limit on passport-github token
|
||||||
var github = new Github({ token: token.token });
|
var github = new Github({ token: githubToken.token });
|
||||||
var repo = github.getRepo('sahat', 'requirejs-library');
|
var repo = github.getRepo('sahat', 'requirejs-library');
|
||||||
repo.show(function(err, repo) {
|
repo.show(function(err, repo) {
|
||||||
res.render('api/github', {
|
res.render('api/github', {
|
||||||
@ -171,6 +139,9 @@ exports.getGithub = function(req, res) {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET /api/twilio
|
||||||
|
*/
|
||||||
exports.getTwilio = function(req, res) {
|
exports.getTwilio = function(req, res) {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user