A Socket.IO middleware for authenticating with a JSON Web Token based on passport-jwt and 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
// Initialize our modulesimport 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 optionsconst 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
Document Hit Me
Tests
npm install
Inspiration
Contribute
You are always welcome to open an issue or provide a pull-request!
License
The GPL-3.0-or-later