Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

socket.io-auth-jwt

MadnessCODE10MIT1.0.0

Socket.IO authentication middleware using JSON Web Token

socket.io, jwt, socket.io middleware, authenticate, authentication, authorize, authorization, auth, Json Web Token, JWT Auth

readme

Socket.io Auth JWT

Build Status

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.