How can I authenticate a socket.io connection? My application uses a login endpoint from another server (python) to get a token, how can I get use that token whenever a user opens a socket connection on the node side?
io.on('connection', function(socket) {
socket.on('message', function(message) {
io.emit('message', message);
});
});
And the client side:
var token = sessionStorage.token;
var socket = io.connect('http://localhost:3000', {
query: 'token=' + token
});
If the token is created in python:
token = jwt.encode(payload, SECRET_KEY, algorithm='HS256')
How can I use this token to authenticate a socket connection in node?
you can use this url.
It doesn't matter if the token was created on another server. You can still verify it if you have the right secret key and algorithm.
Implementation with
jsonwebtoken
moduleclient
Server
Implementation with
socketio-jwt
moduleThis module makes the authentication much easier in both client and server side. Just check out their examples.
client
Server