简体   繁体   中英

How to emit and recieve whitout a Authenticated using socketio-jwt

My Sockets work fine when a client has the token (provided by Laravel jwt) but, a need to work with the clients while they aren't authenticated yet, I Want something like:

io.sockets
.on('connection', socketioJwt.authorize({
  secret: process.env.JWT_SECRET,
  timeout: 15000 // 15 seconds to send the authentication message
}))

.on('authenticated', function(socket) {
  console.log('Authenticated!!');
  // This works:
  socket.on('setDataRegistered', function(datos) {
    console.log('Registering!');
  });
})
// This doesn't works:
.on('config', function(socket) {
  console.log('A client wants to be configured before authenticated!');
})

How can I call from the FrontEnd to 'config' (socket.emit('config')) before authenticate??

Thanks for your help. Sorry my English.

What I do is this:

io.on('connection', function (socket) {
        // Client connected but not authenticated. Client has to emit to 'authenticate' for authentication

        socket.on('authenticate', function (data, fn) {
            // Authenticate socket.

            // define more events
        });

        // still accessible to unauthorised sockets
        socket.on("other-event", function(data) {

        });
});

I don't use any middleware for authentication. But that's just me. I never found the use for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM