Created
February 19, 2012 20:21
-
-
Save sandfox/1865578 to your computer and use it in GitHub Desktop.
Nodejs Auth example
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 app = connect.createServer( | |
//connect.logger(), | |
connect.cookieParser(), | |
connect.session({secret : 'wibble', key : 'phoenix.sid', store: sessionStore}), | |
connect.bodyParser(), | |
//Move this out of here later for cleanliness | |
connect.router(function(app){ | |
app.post('/auth', function(req, res) { | |
/** | |
* parse for the password + username (supplied as json blob) | |
* if they match auth, if not return 401 | |
*/ | |
req.session.authed = true; | |
}); | |
}), | |
//This must go after me login stuff - serves up the static content | |
connect.static(__dirname + '/html') | |
); | |
app.listen(config.get('server:port')); | |
//Socket IO magic happens here | |
var sio = socketio.listen(app); | |
sio.set('authorization', function (data, accept) { | |
if(data.headers.cookie) { | |
var cookie = connect.utils.parseCookie(data.headers.cookie); | |
data.sessionID = cookie['phoenix.sid']; | |
sessionStore.get(data.sessionID, function (err, session) { | |
if(err || !session) { | |
accept('No session found'); | |
} else { | |
if(session.authed != true){ | |
accept('Not authenticated', false); | |
} else { | |
data.session = session; | |
accept(null, true); | |
} | |
} | |
}); | |
} else { | |
return accept('No Cookie Supplied', false); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment