Skip to content

Instantly share code, notes, and snippets.

@ashnur
Created October 27, 2012 20:41
Show Gist options
  • Select an option

  • Save ashnur/3966140 to your computer and use it in GitHub Desktop.

Select an option

Save ashnur/3966140 to your computer and use it in GitHub Desktop.
node static server with passport and dnode and show and whatever
void function(root){
"use strict"
var express = require('express')
, app = express()
, shoe = require('shoe')
, dnode = require('dnode')
, passport = require('passport')
, BrowserIDStrategy = require('passport-browserid').Strategy
, collections = ['peon-users']
, db = require("mongojs").connect('peons', collections)
, redis = require('redis').createClient()
, redsess = require('connect-redis')(express)
, browserid = new BrowserIDStrategy(
{
audience: 'http://localhost:8080'
}
, function(email, done){
(function(err, user){
return done(err, user)
}(undefined, {email: email}))
}
)
, api = {
auth : function (sid, cb) {
console.log(sid, 'sess:'+sid.substring(2,sid.indexOf('.')))
redis.get("sess:"+sid.substring(2,sid.indexOf('.')), function(err, reply) {
// reply is null when the key is missing
if ( reply != null ) {
cb(true)
} else {
cb(false)
}
});
}
}
, config = function (stream) {
var d = dnode(api);
d.pipe(stream).pipe(d)
}
, sock = shoe(config)
, static_middleware
;
redis.on("error", function (err) {
console.log("Redis Error " + err)
})
passport.use(browserid)
passport.serializeUser(function(user, done) {
done(null, user.email)
})
passport.deserializeUser(function(id, done) {
User.findOne(id, function (err, user) {
done(err, user)
})
})
app.use(express.cookieParser())
app.use(express.bodyParser())
app.use(passport.initialize())
app.use(passport.session())
app.use(express.session({ store: new redsess()
, secret: 'secret'
, cookie: { path: '/'
, httpOnly: false
, maxAge: 14400000 } }))
app.use(app.router)
// app.get('/failedLogin', function(req, res){
// res.writeHead(200)
// res.end('y u fail?')
// })
app.post(
'/auth/browserid'
, passport.authenticate('browserid', {
failureRedirect: '/failedLogin'
, failureFlash: true
})
, function(req, res) {
res.redirect('/')
}
)
static_middleware = express.static(__dirname + '/out')
app.use(static_middleware)
// app.get('/', function(req, res, next){
// if ( req.isAuthenticated() ) {
// console.log(1)
// } else {
// console.log(0)
// }
// static_middleware(req, res, next)
// })
app.get('/logout', function(req, res){
req.logOut()
res.redirect('/')
})
sock.install(app.listen(8080), '/test')
}(this)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment