title: Passport + Socket.io get logged user id
body: <p>I'm making a Node.js app and I need to create a new document in mongoDB inside socket.io.
  I have to insert the logged in user in the new document.</p>
  
  <p>How can I access the session variable inside socketio ?</p>
  
  <pre><code>// INCLUDE MODULES =======================================================
  var SocketIOFileUpload = require("socketio-file-upload");
  var express = require('express');
  var app = express();
  var server = require('http').createServer(app);
  var socketio = require('socket.io');
  var Twig = require('twig');
  var twig = Twig.twig;
  var path = require('path');
  var mongoose = require('mongoose');
  var passport = require('passport');
  var Cookies = require( "cookies" );
  var flash    = require('connect-flash');
  var configDB = require('./config/database.js');
  var connect = require('connect');
  
  var ImageFile = require('./app/models/image');
  
  // Assets ================================================================
  app.use(express.static(path.join(__dirname, 'public')));
  app.use(connect.favicon(path.join(__dirname, 'public/images/favicon.ico'))); 
  // Start mongoose
  mongoose.connect(configDB.url);
  
  // USER MANAGEMENT =======================================================
  require('./config/passport')(passport); // pass passport for configuration
  app.use(connect.logger('dev')); // log every request to the console
  app.use(connect.cookieParser()); // read cookies (needed for auth)
  app.use(connect.json());       // to support JSON-encoded bodies
  app.use(SocketIOFileUpload.router);         //socket io file upload
  app.use(connect.urlencoded()); // to support URL-encoded bodies
  app.set('view engine', 'twig'); // set up twig for templating
  app.use(connect.session({ secret: 'ilovescotchscotchyscotchscotch' })); // session secret
  app.use(passport.initialize());
  app.use(passport.session()); // persistent login sessions
  app.use(flash());
  
  // Some more code ...
  
  //SOCKET IO =======================================================
  //Quand on client se connecte, on le note dans la console
  var io = socketio.listen(server);
  io.sockets.on("connection", function(socket){
          // Make an instance of SocketIOFileUpload and listen on this socket:
          var session = socket.handshake.session;
          var uploader = new SocketIOFileUpload();
          uploader.dir = "/LeafStore/public/uploads";
          uploader.listen(socket);
          console.log(socket.handshake.user);
  
          // Do something when a file is saved:
          uploader.on("saved", function(event, socket){
              console.log(event.file);
          });
  
       // Do something when a file is saved:
          uploader.on("complete", function(event){
              console.log("saving file" + name);
              console.log(event);
              var name = event.file.name;
              var imageData = {
                  name: name
                , type: "image"
                , userId: ""
                , creationDate: new Date()
              };
              var newImage = new ImageFile(imageData);
              newImage.save( function(error, data){
                  if(error){
                      throw error;
                  }
                  else{
  
                  }
              });
  
              // save the user
              newImage.save(function(err) {
                  if (err)
                      throw err;
              });;
          });
  
          // Error handler:
          uploader.on("error", function(event){
              console.log("Error from uploader", event);
          });
  });
  
  
  // LISTEN SERVER =======================================================
  server.listen(80);
  </code></pre>