Skip to content

Instantly share code, notes, and snippets.

@PatrickHeneise
Created March 20, 2012 06:38
Show Gist options
  • Save PatrickHeneise/2132062 to your computer and use it in GitHub Desktop.
Save PatrickHeneise/2132062 to your computer and use it in GitHub Desktop.
passport.js with flatiron.js, union and director
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');
});
@robert52
Copy link

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