Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/neutron92/types-passport-jwt.socketio

A typescript passport-jwt middleware for socket.io
https://github.com/neutron92/types-passport-jwt.socketio

authentication node nodejs socketio ts typescript

Last synced: 5 days ago
JSON representation

A typescript passport-jwt middleware for socket.io

Awesome Lists containing this project

README

        

A [Socket.IO](https://socket.io/) middleware for authenticating with a [JSON Web Token](http://jwt.io) based on [passport-jwt](https://github.com/themikenicholson/passport-jwt) and [typeScript](https://github.com/microsoft/TypeScript) .

This module lets you authenticate socket.io endpoints using a JSON web token. It is
intended to be used to secure endpoints without sessions.

## Example usage

### Server
```TypeScript
// Initialize our modules
import socketIO from 'socket.io';
import passportSocketIoTs from "passport-jwt.socketio.ts";

const server = http.createServer(router);

export const ioSocket : socketIO.Server = socketIO(server);

// set the passport-jwt options
const options = {
jwtFromRequest: ExtractJwt.fromUrlQueryParameter('token'),
secretOrKey: secret
}

let passSocketIo: passportSocketIoTs = new passportSocketIoTs();

let verify=(jwtPayload : any, done : any)=>{
// token is valid we still can verify the token
console.log('jwtPayload', jwtPayload)
// the user passed is set to socket.request.user
done(null, jwtPayload)
}

// set the authorization middleware
ioSocket.use(passSocketIo.authorize(options,verify,(err: any) => {
// calllback to log errors
}));

ioSocket.on('connection', function (socket) {
// Connection now authenticated to receive further events
socket.on('ping', function (message) {
socket.emit('pong', message);
});
});

```

### Client
```html




Document

Hit Me



const jwt = 'eyJhbGciOiJIU...';

const socket = io.connect('http://localhost:3001', {
query: {
token: jwt // your token
}
});

function sendMsg() {
socket.emit('messages', 'hello!');
socket.on('messages', (data) => {
console.log('authenticate', data)
});
}

```

## Tests

npm install

## Inspiration

* [passport-jwt.socketio](https://github.com/erreina/passport-jwt.socketio)

## Contribute

You are always welcome to open an issue or provide a pull-request!

## License

The [GPL-3.0-or-later](https://www.gnu.org/licenses/gpl-3.0.fr.html)