Created
March 31, 2012 16:26
-
-
Save drinchev/2266544 to your computer and use it in GitHub Desktop.
Integrate NowJS with Express and Passport
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 express = require('express') | |
, passport = require('passport') | |
, sessionStore = new express.session.MemoryStore() | |
, app = module.exports = express.createServer() | |
, nowjs = require('now'); | |
app.configure( function () { | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(express.cookieParser('connect')); | |
app.use(express.session({ secret : "connect", store : sessionStore })); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
app.use(express.errorHandler({dumpExeptions: true, showStack : true})); | |
app.use(app.router); | |
}); | |
nowjs.on('connect', function() { | |
var self = this; | |
if ( this.user.cookie && this.user.cookie['connect.sid'] ) { | |
var sid = decodeURIComponent( this.user.cookie['connect.sid'] ); | |
sessionStore.get( sid, function( err, session ) { | |
if ( session ) { | |
passport.deserializeUser(session.passport.user, function(err, user) { | |
self.user.session.passport = user; | |
}); | |
} | |
}); | |
}; | |
}); | |
var everyone = nowjs.initialize(app); | |
app.listen(3000); | |
console.log('app is listening'); | |
everyone.now.getUsername( function (cb) { | |
if ( this.user.session && this.user.session.passport && this.user.session.username ) cb(this.user.session.username); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Of course you should have written your passport Strategy file and authentication mechanism.