UNPKG

1.27 kBJavaScriptView Raw
1const debug = require('debug')('@feathersjs/socketio/middleware');
2
3exports.disconnect = (app, getParams) => (socket, next) => {
4 socket.once('disconnect', () => app.emit('disconnect', getParams(socket)));
5 next();
6};
7
8exports.params = (app, socketMap) => (socket, next) => {
9 socket.feathers = {
10 provider: 'socketio',
11 headers: socket.handshake.headers
12 };
13
14 socketMap.set(socket.feathers, socket);
15
16 next();
17};
18
19exports.authentication = (app, getParams, settings = {}) => (socket, next) => {
20 const service = app.defaultAuthentication ? app.defaultAuthentication(settings.service) : null;
21
22 if (service === null) {
23 return next();
24 }
25
26 const config = service.configuration;
27 const authStrategies = config.parseStrategies || config.authStrategies || [];
28
29 if (authStrategies.length === 0) {
30 return next();
31 }
32
33 service.parse(socket.handshake, null, ...authStrategies)
34 .then(async authentication => {
35 if (authentication) {
36 debug('Parsed authentication from HTTP header', authentication);
37 socket.feathers.authentication = authentication;
38 await service.create(authentication, {
39 provider: 'socketio',
40 connection: getParams(socket)
41 });
42 }
43
44 next();
45 }).catch(next);
46};