title: socket.request.user undefined in Passport.js, Socket.io, and Express
body: <p>I am learning NodeJs, Express, and Socket.io through a tutorial, the tutorial used a previous version of NodeJs, Express, and Socket.io to access userID. I am using current versions to do the same but it wasn't successful, I adjusted my code to conform with the current version but what I get is an empty string.</p>
  <p>SESSION:</p>
  <pre><code>&quot;use strict&quot;;
  const session = require(&quot;express-session&quot;);
  const MongoStore = require(&quot;connect-mongo&quot;)(session);
  const config = require(&quot;../config&quot;);
  const db = require(&quot;../db&quot;);
  
  if (process.env.NODE_ENV === &quot;production&quot;) {
    // Initialize session with settings for production
    module.exports = session({
      secret: config.sessionSecret,
      resave: false,
      saveUninitialized: false,
      store: new MongoStore({
        mongooseConnection: db.Mongoose.connection,
      }),
    });
  } else {
    module.exports = session({
      secret: config.sessionSecret,
      resave: false,
      saveUninitialized: true,
      store: new MongoStore({
        mongooseConnection: db.Mongoose.connection,
      }),
    });
  }
  </code></pre>
  <p>SERVER</p>
  <pre><code>&quot;use strict&quot;;
  const express = require(&quot;express&quot;);
  const ChatCAT = require(&quot;./app&quot;);
  const passport = require(&quot;passport&quot;);
  
  const app = express();
  app.set(&quot;port&quot;, process.env.PORT || 3000);
  app.use(express.static(&quot;public&quot;));
  app.set(&quot;view engine&quot;, &quot;ejs&quot;);
  
  app.use(ChatCAT.session);
  app.use(passport.initialize());
  app.use(passport.session());
  app.use(&quot;/&quot;, ChatCAT.router);
  
  let port = app.get(&quot;port&quot;);
  ChatCAT.ioServer(app, passport).listen(port, () =&gt; {
    console.log(&quot;ChatCAT Running on Port: &quot;, port);
  });
  </code></pre>
  <p>SOCKET.IO</p>
  <pre><code> io.of(&quot;/chatter&quot;).on(&quot;connection&quot;, (socket) =&gt; {
      // Join a chatroom
      socket.on(&quot;join&quot;, (data) =&gt; {
        let usersList = helper.addUserToRoom(allrooms, data, socket);
  
        // Update the list of active users as shown on the chatroom page
        console.log(&quot;usersList: &quot;, usersList);
        // console.log(&quot;sessionID: &quot;, socket.request.session);
      });
    });
  </code></pre>
  <p>addUserToRoom FUNCTION</p>
  <pre><code>let addUserToRoom = (allrooms, data, socket) =&gt; {
    // Get the room object
    let getRoom = findRoomById(allrooms, data.roomID);
    if (getRoom !== undefined) {
      // Get the active user's ID(ObjectID as used in session)
      // let userID = socket.request.session.passport.user;
      let userID = socket.request.user ? socket.request.user.id : &quot;&quot;;
  
      //let userID = socket.request.session.passport.user;
      // Check to see if this user already exist in the chatroom
      let checkUser = getRoom.users.findIndex((element, index, array) =&gt; {
        // console.log(&quot;elementID: &quot;, element.userID);
        if (element.userID === userID) {
          return true;
        } else {
          return false;
        }
      });
  
      // If the user is already present in the room, remove the user first
      if (checkUser &gt; -1) {
        getRoom.users.splice(checkUser, 1);
      }
  
      // Push the user into the room's users array
      getRoom.users.push({
        socketID: socket.id,
        userID,
        user: data.user,
        userPic: data.userPic,
      });
  
      // Join the room channel
      socket.join(data.roomID);
  
      // Return the updated room object
      return getRoom;
    }
  };
  </code></pre>
  <p>Create an IO Server instance</p>
  <pre><code>let ioServer = (app, passport) =&gt; {
    app.locals.chatrooms = [];
    const server = require(&quot;http&quot;).createServer(app);
    const io = require(&quot;socket.io&quot;)(server);
    const wrap = (middleware) =&gt; (socket, next) =&gt;
      middleware(socket.request, {}, next);
    io.use(wrap(require(&quot;./session&quot;)));
    io.use(wrap(passport.initialize()));
    io.use(wrap(passport.session()));
  
    io.use((socket, next) =&gt; {
      if (socket.request.user) {
        next();
      } else {
        next(new Error(&quot;unauthorized&quot;));
      }
    });
  
    require(&quot;./socket&quot;)(io, app);
    return server;
  };
  </code></pre>
  <p>You can see in the image below that I get an empty string in the userID
  <a href="https://i.stack.imgur.com/wM1Qj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wM1Qj.png" alt="enter image description here" /></a></p>
  <p><strong>UPDATE</strong></p>
  <p>I adjusted my code to use passport.socketio but I am still getting the same blank string in my UserID, when I print socket.request to the console I get some data but when I print socket.request.user I get undefined:</p>
  <pre><code>let ioServer = (app, cookieParser) =&gt; {
    app.locals.chatrooms = [];
    const config = require(&quot;./config&quot;);
    const server = require(&quot;http&quot;).Server(app);
    const io = require(&quot;socket.io&quot;)(server);
    const passportSocketIo = require(&quot;passport.socketio&quot;);
    const sessionStore = require(&quot;./session&quot;);
    const helper = require(&quot;./helpers&quot;);
  
    io.use(
      passportSocketIo.authorize({
        cookieParser: cookieParser,
        key: &quot;connect.sid&quot;,
        secret: config.sessionSecret,
        store: sessionStore.store,
        success: helper.onAuthorizeSuccess,
        fail: helper.onAuthorizeFail,
      })
    );
  
    const socket = require(&quot;./socket&quot;);
    socket(io, app);
    return server;
  };
  </code></pre>