Create a gist now

Instantly share code, notes, and snippets.

Integrate NowJS with Express and Passport
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);
});
@drinchev
Owner

Of course you should have written your passport Strategy file and authentication mechanism.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment