0

I got a middlware to validate my socket by jwt but I have a problem making new events in other folders Not to get everything in my app.js

I did this to authenticate my socket:

  let io = require('socket.io')(server);
  app.set("io", io);

  io.use(verify.passportJwtAuth);
  io.on('connection', function(socket) {
    console.log('Authentication passed!');
    // now you can access user info through socket.request.user
    // socket.request.user.logged_in will be set to true if the user was authenticated
    socket.emit('success', {
      message: 'success logged in!'
    });
  });

midd:

async passportJwtAuth(socket,next) {
    const secret =  config.jwt.secret;
    const token = socket.handshake.query.token;
    jwt.verify(token, secret, function(err, decoded) {
        if(err) {
            return next(err);
        }
        console.log("sucess");
        return next();
    });
} 

I've tried to make a new event:

const socketRouter = require('./routes/socket');
  io.use(verify.passportJwtAuth);
  io.on('connection', function(socket) {
    console.log('Authentication passed!');
    // now you can access user info through socket.request.user
    // socket.request.user.logged_in will be set to true if the user was authenticated
    socket.emit('success', {
      message: 'success logged in!'
    });
  });
socketRouter.configuration();

my socketRouter.configuration:

const socketController = require ('../controllers/SocketController');
const verify = require('../auth/index');
const socket = require('socket.io');
module.exports = {    
   configuration: () => {        
      socket.on ('message', (message) => {
         socket.emit ('myevent', {message: 'Ok, You are signed in'});
         console.log (message);
         console.log (`Socket $ {socket.id}`)
       });
   }
}

error:

C:\Users\SpiriT\Documents\Projetos\FtcJokenPo\back\src\routes\socket.js:6
      socket.on ('message', (message) => {
             ^

TypeError: socket.on is not a function
    at Object.configuration (C:\Users\SpiriT\Documents\Projetos\FtcJokenPo\back\src\routes\socket.js:6:14)
    at Object.<anonymous> (C:\Users\SpiriT\Documents\Projetos\FtcJokenPo\back\src\app.js:36:14)

I didn't want to have to leave all my socket logic in my app.js

Also this way soon when I turn on the server I already call this module (I wanted it to call only when I use socket.on on the front end

Could someone give me a hand?

my front end:

verify = () => {
    let socket = null;
    var token = 312312312;
    socket = io('http://localhost:8080', {
    query: {token: token}
    });
    socket.on('error', function(err) {
       console.error(err);
      });
      // Connection succeeded
      socket.on('success', function(data) {
        console.log(data.message);
      })
}
Dupocas
  • 20,285
  • 6
  • 38
  • 56

1 Answers1

0

In your socketRouter.configuration file, with this code below, your socket variable is not what you need it to be. It looks like you want it to be a specific connected socket that you can listen for incoming messages on. You are trying to use:

 const socket = require('socket.io');

to get that socket, but that's simply the export object from the socket.io module and has nothing to do with an existing connected socket.

What it appears you need to do is to pass a particular socket to your configuration function and then use that argument in your function. First change the configuration module to this:

const socketController = require ('../controllers/SocketController');
const verify = require('../auth/index');
module.exports = {    
   configuration: (socket) => {        
      socket.on ('message', (message) => {
         socket.emit ('myevent', {message: 'Ok, You are signed in'});
         console.log (message);
         console.log (`Socket $ {socket.id}`)
       });
   }
}

This removes the require('socket.io') line and adds a socket argument to the configuration function.


Then, where that configuration() function is called, you need to pass that socket argument:

  const socketRouter = require('./routes/socket');
  io.use(verify.passportJwtAuth);
  io.on('connection', function(socket) {
    console.log('Authentication passed!');
    // now you can access user info through socket.request.user
    // socket.request.user.logged_in will be set to true if the user was authenticated
    socket.emit('success', {
      message: 'success logged in!'
    });

    // let the socketRouter add any message handlers it wants to for this socket
    socketRouter.configuration(socket);      
  });
jfriend00
  • 683,504
  • 96
  • 985
  • 979