Created
March 20, 2012 06:38
-
-
Save PatrickHeneise/2132062 to your computer and use it in GitHub Desktop.
passport.js with flatiron.js, union and director
This file contains 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
var flatiron = require('flatiron') | |
, connect = require('connect') | |
, path = require('path') | |
, fs = require('fs') | |
, plates = require('plates') | |
, director = require('director') | |
, util = require('util') | |
, keys = require('./auth_keys') | |
, passport = require('passport') | |
, TwitterStrategy = require('passport-twitter').Strategy | |
, union = require('union'); | |
passport.use(new TwitterStrategy({ | |
consumerKey: keys.twitter.consumerKey, | |
consumerSecret: keys.twitter.consumerSecret, | |
callbackURL: "http://127.0.0.1:3000/auth/twitter/callback" | |
}, | |
function(token, tokenSecret, profile, done) { | |
console.log("strategy"); | |
// asynchronous verification, for effect... | |
process.nextTick(function () { | |
console.log("strategy tick"); | |
return done(null, profile); | |
}); | |
} | |
)); | |
passport.serializeUser(function(user, done) { | |
console.log("serialize"); | |
done(null, user); | |
}); | |
passport.deserializeUser(function(obj, done) { | |
console.log("deserialize"); | |
done(null, obj); | |
}); | |
var router = new director.http.Router(); | |
var server = union.createServer({ | |
before: [ | |
connect.cookieParser("secret"), | |
connect.session(), | |
passport.initialize(), | |
passport.session(), | |
function (req, res) { | |
var found = router.dispatch(req, res); | |
if (!found) { | |
res.emit('next'); | |
} | |
}, | |
connect.static('public') | |
] | |
}); | |
router.get('/auth/twitter', | |
passport.authenticate('twitter'), | |
function(){} | |
); | |
router.get('/auth/twitter/callback', | |
passport.authenticate('twitter', { failureRedirect: '/login' }), | |
function() { | |
this.res.writeHead(302, { | |
'Location': 'login.html' | |
}); | |
this.res.end(); | |
}); | |
router.get(/logout/, function() { | |
// req.logout(); | |
this.res.writeHead(302, { | |
'Location': '/' | |
}); | |
this.res.end(); | |
}); | |
// GET / | |
// Main function | |
router.get('/', function () { | |
console.log("GET /"); | |
var self = this; | |
// fs.get etc. | |
}) | |
}); | |
// Simple route middleware to ensure user is authenticated. | |
// Use this route middleware on any resource that needs to be protected. If | |
// the request is authenticated (typically via a persistent login session), | |
// the request will proceed. Otherwise, the user will be redirected to the | |
// login page. | |
function ensureAuthenticated(req, res, next) { | |
if (req.isAuthenticated()) { return next(); } | |
res.redirect('/login') | |
} | |
server.listen(3000, function () { | |
console.log('Application is now started on port 3000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I'm having some trouble the debugger says that "req" has no method "isAuthenticated". I'm checking if the user is authenticated in the before hook to not go all the way up to the app's router. What do you think that the problem might be.
Robert.