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'); | |
}); |
I believe I got this working with a simple package.
https://npmjs.org/package/flatiron-passport
https://github.com/travist/flatiron-passport
Please read the README.md on how to use. For the most part, it is a pretty simple wrapper around passport that makes it work within flatiron. I have only so far tested LocalStrategy, but it should work with others since I basically maintained the API between the two. Please test this and let me know if this works for you guys.
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.
app.use(flatiron.plugins.http, {
before : [
function(req, res) {
req.originalUrl = req.url;
res.emit('next');
},
connect.cookieParser(),
connect.session({ secret : 'keyboard cat' }),
passport.initialize(),
passport.session(),
function(req, res) {
if (!/^\/dashboard$|dashboard\/.*$/.test(req.url)) {
return res.emit('next');
}
if (req.isAuthenticated()) {
return res.emit('next');
}
res.redirect('/');
},
ecstatic(path.join(__dirname, './public'), {
autoIndex : false,
cache : "0, no-cache, no-store, must-revalidate"
}) //cache control was turned off
]
});
Robert.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Steele, check out this branch of Passport:
https://github.com/jaredhanson/passport/tree/flatiron
I am currently using the two together, however, this is only for plain authentication. I have yet to try the oAuth, but I dont see any reason why it would not work.
However, just keep in mind that you will have to use a few tricks as Jared mentioned above to get it working. Here are the relevant snippets from my code: