-2

Server side:

var express = require('express');
var http = require('http');
var path = require('path');
var socketIO = require('socket.io');
var socketioJwt = require('socketio-jwt');
var app = express();
var server = http.Server(app);
var io = socketIO(server);
app.use('/files', express.static(__dirname + '/files'));
app.get('/', function(request, response) {
    response.sendFile('G:/jwtclient.html');
});
// Accept connection and authorize token
io.on('connection', socketioJwt.authorize({
    secret: 'SECRET_KEY',
    timeout: 15000,
}));
io.on('authenticated', function (socket) {
    console.log(socket.decoded_token);
    console.log('Authenticated!');
});
server.listen(1111);

Client side:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Js1</title>
  <link href="" rel="stylesheet" type="text/css">
  <script src="/socket.io/socket.io.js"></script>
</head>
<body>
    <p id = 'test'></p>
    <script>var token = sessionStorage.token;
var socket = io('localhost:1111');
socket.on('connect', function () {
    console.log('connected!');
  socket.on('authenticated', function () {
      document.getElementById('test').innerHTML = 'Authenticated!!!';
    });
  socket.emit('authenticate', {token: token}); //send the jwt
});
</script>
</body>
</html>

I do get a 'connected!' log in my console, but then the authentication does not seem to take place. I can't figure out what I'm doing wrong. Any help would be greatly appreciated.

Edit: I also tried this:

Server:

var express = require('express');
var http = require('http');
var path = require('path');
var socketIO = require('socket.io');
var socketioJwt = require('socketio-jwt');
var app = express();
var server = http.Server(app);
var io = socketIO(server);
app.use('/files', express.static(__dirname + '/files'));
app.get('/', function(request, response) {
    response.sendFile('G:/jwtclient.html');
});
io.use(socketioJwt.authorize({
  secret: 'secret',
  handshake: true,
}));
// Accept connection and authorize token
io.on('connection', function() {
    console.log('authorized');
});
server.listen(1111);

Client:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Js1</title>
  <link href="" rel="stylesheet" type="text/css">
  <script src="/socket.io/socket.io.js"></script>
</head>
<body>
    <p id = 'test'></p>
    <script>var token = sessionStorage.token;
var socket = io.connect('http://localhost:1111', {
  'query': 'token=' + token,
});
socket.on('connect', function (sock) {
    console.log('connected!');
});
</script>
</body>
</html>

Both exactly like in the official documentation here: https://www.npmjs.com/package/socketio-jwt

Nothing works. Is the package simply deprecated and rendered useless or am I doing something wrong? Because I am copying everything from the documentation...

Randomdude
  • 101
  • 2
  • 9

1 Answers1

-3

On server side you should accept and validate JWT like this:

socket.on("authenticate", function(jwt){

      socket.emit("authenticated"); // if autehnticated
})
Tushar Nikam
  • 594
  • 4
  • 13
  • I did not downvote, but this does not work. It also goes against the examples in the documentation: https://www.npmjs.com/package/socketio-jwt Also the examples here: https://stackoverflow.com/questions/36788831/authenticating-socket-io-connections – Randomdude Jul 13 '18 at 11:00
  • Not at all. I am doing everything exactly like the official documentation says and still nothing works... – Randomdude Jul 13 '18 at 11:08