0

In fact, I want to restrict users from connecting to the Socket before logging into the Web.

For api calls, I can use passport.js(session) and do the following to restrict them from using the api before logging in

app.get('/login', checkAuth, (req, res) => {
  res.status(200).send('done');
})

function checkAuth(req, res, next) {
  if (req.isAuthenticated()) {
    return res.redirect('/')
  }
  next()
}

But how can I restrict socket.io connections before logging in, or perform an (auth) operation similar to the above?

  io.sockets.on('connection', function (socket) {
    socket.on('hi', async data => {
      await socket.join('XP');
    });
  });
JustFun2020
  • 35
  • 1
  • 4
  • Check this: https://stackoverflow.com/questions/36788831/authenticating-socket-io-connections-using-jwt – Daniel Sep 14 '20 at 10:03
  • @Daniel I have read it before, but I use Session in the system. Is there any libraries that can do something similar to allow socket.io to check authentication? – JustFun2020 Sep 14 '20 at 12:59
  • 1
    How about this https://stackoverflow.com/questions/25532692/how-to-share-sessions-with-socket-io-1-x-and-express-4-x ? – Daniel Sep 14 '20 at 13:14

0 Answers0