From 58457a58e7df922c5d02f52f76cf00b75f9de44e Mon Sep 17 00:00:00 2001 From: Sahat Yalkabov Date: Tue, 19 Nov 2013 17:09:05 -0500 Subject: [PATCH] Updated foursquare routes for authorization. Now properly retrives the Oauth token when app is authorized --- app.js | 10 ++++------ controllers/api.js | 39 ++++++++++++++++++++++++++++----------- package.json | 4 +++- views/api/foursquare.jade | 4 +++- 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/app.js b/app.js index a3d3d400f5..75140409ff 100755 --- a/app.js +++ b/app.js @@ -6,6 +6,7 @@ var express = require('express'), mongoose = require('mongoose'), passport = require('passport'); +// TODO: Add node-opencv!! // App Configuration (API Keys, Database URI) var config = require('./config/config.json'); @@ -67,19 +68,16 @@ app.post('/contact', contact.postContact); -// Redirect the user to Facebook for authentication. When complete, -// Facebook will redirect the user back to the application at app.get('/auth/facebook', passport.authenticate('facebook')); -// Facebook will redirect the user to this URL after approval. Finish the -// authentication process by attempting to obtain an access token. If -// access was granted, the user will be logged in. Otherwise, -// authentication has failed. app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/login' })); +app.get('/auth/foursquare', api.foursquareAuth); +app.get('/auth/foursquare/callback', api.foursquareCallback); + app.get('*', home.index); diff --git a/controllers/api.js b/controllers/api.js index 54b118051e..a64d943a98 100644 --- a/controllers/api.js +++ b/controllers/api.js @@ -1,5 +1,7 @@ var config = require('../config/config.json'); +var User = require('../models/User'); + // API PROVIDERS SETUP var foursquare = require('node-foursquare')({ secrets: { @@ -9,26 +11,41 @@ var foursquare = require('node-foursquare')({ } }); +var foursquareAccessToken = 'MY_FOURSQUARE_ACCESS_TOKEN'; + + exports.apiBrowser = function(req, res) { res.render('api'); }; + exports.foursquare = function(req, res) { + res.render('api/foursquare', { title: 'Foursquare API' }); + }; + +/** + * 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 + */ exports.foursquareCallback = function(req, res) { - foursquare.getAccessToken({ - code: req.query.code - }, - function(err, accessToken) { - if (err) { - res.send('An error was thrown: ' + err.message); - } - else { - // Save the accessToken and redirect. - } + foursquare.getAccessToken({ code: req.query.code }, function(err, accessToken) { + if (err) throw err; + + console.log(accessToken); }); -}); \ No newline at end of file +}; + diff --git a/package.json b/package.json index 215ec7f075..6494c57070 100755 --- a/package.json +++ b/package.json @@ -12,6 +12,8 @@ "passport-google": "latest", "underscore": "latest", "forever": "latest", - "node-foursquare": "latest" + "node-foursquare": "latest", + + "opencv": "latest" } } \ No newline at end of file diff --git a/views/api/foursquare.jade b/views/api/foursquare.jade index 6e6bdb2443..bcd5c90c78 100644 --- a/views/api/foursquare.jade +++ b/views/api/foursquare.jade @@ -1,4 +1,6 @@ extends ../layout block content - h1 Foursquare API \ No newline at end of file + h1 Foursquare API + + a.btn.btn-large.btn-primary(href='/auth/foursquare') Get Foursquare Token \ No newline at end of file