socket.io-auth-jwt
v1.0.0
Published
Socket.IO authentication middleware using JSON Web Token
Downloads
5
Maintainers
Readme
Socket.io Auth JWT
Socket.IO authentication middleware using JSON Web Token
Installation
The Socket.io Auth JWT can be installed with NPM. Run this command:
npm i socket.io-auth-jwt
Using
Server side:
io.use(new authJWT({
secret: 'your secret key',
error_details: true,
error_event: "unauthorized_error"
})).on("connection", (socket) => {
if(this.socket.handshake.user == undefined){
//Redirect user to login page
this.socket.disconnect(true);
}
console.log(this.socket.handshake.user);
});
Note: Only secret parametar is required. By default error_details:
false
and error_event is 'unauthorized
'
Client side:
var your_jwt_token = "xxxxx.xxxx.xxxx";
var socket = io('http://localhost:3000', {query: "token="+your_jwt_token});
socket.on('unauthorized_error', function (err) {
console.log(err);
});
License
Please see the license file for more information.