Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/neutron92/types-passport-jwt.socketio
- Owner: neutron92
- License: gpl-3.0
- Created: 2019-11-14T20:27:23.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-09T20:21:54.000Z (almost 2 years ago)
- Last Synced: 2024-10-12T08:26:24.880Z (about 1 month ago)
- Topics: authentication, node, nodejs, socketio, ts, typescript
- Language: TypeScript
- Size: 4.29 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
DocumentHit 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)