0.1.0 • Published 5 years ago

@h.schulz/socketio-auth-typescript v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

socketio-auth-typescript

This package provides authentication for Socket.IO

Installing

The install ist pretty forward. Just install the npm package and you're finished.

npm -i @hschulz/socketio-auth-typescript

Usage

Server

import { authenticateSocket } from "@hschulz/socketio-auth-typescript"

const server = createServer().listen(56789)
const sio: socketio.Server = socketio.listen(server)

const onAuthenticate = (socket: SocketIO.Socket, data: any, callback: (err?: Error, success?: boolean) => void) => {

    /* Data will be whatever the client sent */
    if (!data) {
        return callback(new Error('Authentication error - No Data'))
    }

    /* However you validate the client data */
    if (data === 'isSomehowValid') {
        return callback(void, true)
    }

    /* No authentication */
    return callback(void, false)
};

authenticateSocket(sio, {
    onAuthenticate
})

Client

var io = require('socket.io-client');

var socket = io.connect('http://localhost:56789');

socket.on("connect", () => {

    socket.emit("authentication", "isSomehowValid");

    socket.on("authenticated", () => {
        // The socket is now authenticated
    });

    socket.on("unauthorized", (message) => {
        // There was an error during the authentication
    });
});

Built With

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

https://github.com/facundoolano/socketio-auth also did this in CommonJS some time ago

0.2.0

5 years ago

0.1.0

5 years ago