socket.io-auth-jwt

    1.0.0 • Public • Published

    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.

    Install

    npm i socket.io-auth-jwt

    DownloadsWeekly Downloads

    1

    Version

    1.0.0

    License

    MIT

    Last publish

    Collaborators

    • avatar