diff --git a/src/auth-socket-middleware.ts b/src/auth-socket-middleware.ts index d3993f2..3c6cb45 100644 --- a/src/auth-socket-middleware.ts +++ b/src/auth-socket-middleware.ts @@ -18,10 +18,16 @@ export default function registerAuthSocketMiddleware(io: Server) { next(new Error()); } logger.info(`Token decoded: ${decoded}`); - const email = typeof decoded === 'string' ? decoded : decoded!.email; - logger.info(`${email} is listening notifications`); - socket.join(email); + if (decoded && typeof decoded !== 'string' && decoded.userId) { + logger.info(`userId: ${decoded.userId} is listening notifications`); + socket.join(decoded.userId); + } else { + const email = typeof decoded === 'string' ? decoded : decoded!.email; + logger.info(`email: ${email} is listening notifications`); + socket.join(email); + } + next(); }); }); diff --git a/src/handler.ts b/src/handler.ts index 262ef2a..4cc306c 100644 --- a/src/handler.ts +++ b/src/handler.ts @@ -5,12 +5,13 @@ import Logger from './logger'; type RequestBody = { event: string; payload: any; - email: string; + email?: string; + userId?: string; clientId?: string; }; function validateBody(body: Record): RequestBody { - if (body.event && body.payload && body.email) return body as RequestBody; + if (body.event && body.payload && (body.email || body.userId)) return body as RequestBody; throw new Error('Body is not in the expected format'); } @@ -28,7 +29,14 @@ export default function registerHandler(app: Express, io: Server) { logger.info(`Event is going to be emited: ${JSON.stringify(body, null, 2)}`); - io.to(body.email).emit('event', body); + if(body.email){ + io.to(body.email).emit('event', body); + } + + if(body.userId){ + io.to(body.userId).emit('event', body); + } + res.status(201).send(); }); }