1

I've recently implemented passport.js for authentication onto my express server. I'm trying to leverage this authentication for my socket.io server as well. So I'm trying to use the passport.socketio package to do this.

I'm using it as such:

io.use(async (socket, next) => {
    console.log("pre auth")

    await passportSocketIo.authorize({
      cookieParser: cookieParser,       // the same middleware you registrer in express
      key:          'auth',       // the name of the cookie where express/connect stores its session_id
      secret:       'keyboard cat',    // the session_secret to parse the cookie
      store:        tediousConfig,        // we NEED to use a sessionstore. no memorystore please
      success:      onAuthorizeSuccess,  // *optional* callback on success - read more below
      fail:         onAuthorizeFail,     // *optional* callback on fail/error - read more below
    })

    console.log("post auth")
}

With the success and fail callbacks being:

function onAuthorizeSuccess(data, accept){
  console.log("Success connection to socket.io");
  accept();
}

function onAuthorizeFail(data, message, error, accept){
  if(error) 
    throw new Error(message);

  console.log("failed connection to web socket", message);
}

However, when I try to connect I get both the console messages of "pre auth" and "post auth" but no message from the authorize function, and no error messages at all.

Any help would be gratefully received!

1 Answers1

2

The namespace.use method is a method that adds a middleware(function) to your socket. So it's expecting a function. In the other hand passportSocketIo.authorize is returning the middleware function you can check the code here. So you have 2 options either you use it like it's specified in the docs:

io.use(passportSocketIo.authorize({
  cookieParser: cookieParser,       // the same middleware you registrer in express
  key:          'express.sid',       // the name of the cookie where express/connect stores its session_id
  secret:       'session_secret',    // the session_secret to parse the cookie
  store:        sessionStore,        // we NEED to use a sessionstore. no memorystore please
  success:      onAuthorizeSuccess,  // *optional* callback on success - read more below
  fail:         onAuthorizeFail,     // *optional* callback on fail/error - read more below
}));

Or you customize it like this:

io.use(async (socket, next) => {
    console.log("pre auth")

    return passportSocketIo.authorize({
      cookieParser: cookieParser,       // the same middleware you registrer in express
      key:          'auth',       // the name of the cookie where express/connect stores its session_id
      secret:       'keyboard cat',    // the session_secret to parse the cookie
      store:        tediousConfig,        // we NEED to use a sessionstore. no memorystore please
      success:      onAuthorizeSuccess,  // *optional* callback on success - read more below
      fail:         onAuthorizeFail,     // *optional* callback on fail/error - read more below
    })(socket, next);
}
Melchia
  • 22,578
  • 22
  • 103
  • 117