Skip to content

Instantly share code, notes, and snippets.

@sandfox
Created February 19, 2012 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandfox/1865578 to your computer and use it in GitHub Desktop.
Save sandfox/1865578 to your computer and use it in GitHub Desktop.
Nodejs Auth example
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