freeCodeCamp/controllers/api.js

68 lines
1.7 KiB
JavaScript
Raw Normal View History

var geoip = require('geoip-lite');
2013-11-19 21:20:18 +00:00
var config = require('../config/config.json');
var User = require('../models/User');
2013-11-19 21:20:18 +00:00
// API PROVIDERS SETUP
var foursquare = require('node-foursquare')({
secrets: {
clientId: config.foursquare.clientId,
clientSecret: config.foursquare.clientSecret,
redirectUrl: config.foursquare.callbackUrl
}
});
var foursquareAccessToken = 'MY_FOURSQUARE_ACCESS_TOKEN';
exports.apiBrowser = function(req, res) {
2013-11-22 15:23:54 +00:00
console.log(req.user);
res.render('api', {
title: 'API Browser',
user: req.user
});
2013-11-19 21:20:18 +00:00
};
2013-11-22 15:23:54 +00:00
// TODO: require foursquare auth when clicking on foursquare url
// being logged in is not enough
2013-11-19 21:20:18 +00:00
exports.foursquare = function(req, res) {
var geo = geoip.lookup('4.17.136.0' || req.connection.remoteAddress);
2013-11-20 03:42:58 +00:00
foursquare.Venues.getTrending(geo.ll[0], geo.ll[1], { limit: 5 }, req.user.tokens.foursquare, function(err, results) {
res.render('api/foursquare', {
title: 'Foursquare API',
user: req.user,
venues: results.venues
});
2013-11-19 21:20:18 +00:00
});
2013-11-19 21:20:18 +00:00
};
/**
* GET /auth/foursquare
* Display Foursquare authentication screen
*/
exports.foursquareAuth = function(req, res) {
res.writeHead(303, { location: foursquare.getAuthClientRedirectUrl() });
res.end();
};
/**
* GET /auth/foursquare/callback
* Called when user presses Accept on the Foursquare authentication screen
*/
2013-11-19 21:20:18 +00:00
exports.foursquareCallback = function(req, res) {
foursquare.getAccessToken({ code: req.query.code }, function(err, accessToken) {
if (err) throw err;
User.findByIdAndUpdate(req.user._id, { $set: { tokens: { foursquare: accessToken } } }, null, function(err, user) {
if (err) throw err;
res.redirect('/api/foursquare');
});
2013-11-19 21:20:18 +00:00
});
};