Created
April 30, 2012 16:20
-
-
Save jaredhanson/2559730 to your computer and use it in GitHub Desktop.
Restify and Passport /cc Hal Robertson
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based off example code from Hal Robertson | |
// https://github.com/halrobertson/test-restify-passport-facebook | |
// See discussion: https://groups.google.com/forum/?fromgroups#!topic/passportjs/zCz0nXB_gao | |
var restify = require('restify') | |
// config vars | |
var FB_LOGIN_PATH = '/api/facebook_login' | |
var FB_CALLBACK_PATH = '/api/facebook_callback' | |
var FB_APPID = '<<YOUR APPID HERE>>' | |
var FB_APPSECRET = '<<YOUR APPSECRET HERE>>' | |
var SERVER_PREFIX = 'http://localhost:3000' | |
// set up server | |
var server = restify.createServer() | |
server.use(restify.queryParser()); | |
// set up passport-facebook | |
var passport = require('passport') | |
, FacebookStrategy = require('passport-facebook').Strategy; | |
// initialize passport | |
server.use(passport.initialize()); | |
// Sessions aren't used in this example. To enabled sessions, enable the | |
// `session` option and implement session support with user serialization. | |
// See here for info: http://passportjs.org/guide/configuration.html | |
var fb_login_handler = passport.authenticate('facebook', { session: false }) | |
var fb_callback_handler = passport.authenticate('facebook', { session: false }) | |
var fb_callback_handler2 = function(req, res) { | |
console.log('we b logged in!') | |
console.dir(req.user) | |
// be sure to send a response | |
res.send('Welcome ' + req.user.displayName); | |
} | |
server.get(FB_LOGIN_PATH, fb_login_handler) | |
server.get(FB_CALLBACK_PATH, fb_callback_handler, fb_callback_handler2) | |
passport.use(new FacebookStrategy({ | |
clientID: FB_APPID, | |
clientSecret: FB_APPSECRET, | |
callbackURL: SERVER_PREFIX + FB_CALLBACK_PATH | |
}, | |
function(accessToken, refreshToken, profile, done) { | |
console.log('accessToken='+accessToken+' facebookId='+profile.id) | |
return done(null, profile) | |
}) | |
) | |
// Start the app by listening on <port> | |
var port = process.env.PORT || 3000 | |
server.listen(port) | |
console.log('App started on port ' + port) |
Doing this just keeps redirecting me to log in each time.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@thatguynamedandy the same happened to me, and I had to be explicit in the property that will hold the user object, due to some issue with passport.js:
With that change everything worked ok ^^