From 1e766ace7cd13c4f8e4410c6dcb6674219a94b3b Mon Sep 17 00:00:00 2001 From: Fabio Espinosa Date: Wed, 10 Aug 2022 10:52:24 +0200 Subject: [PATCH 1/3] refactor(handler): receive userId or email --- src/handler.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/handler.ts b/src/handler.ts index 262ef2a..d289957 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'); } From f65756fa1d5057b296b6e88d4146d432a4c7f522 Mon Sep 17 00:00:00 2001 From: Fabio Espinosa Date: Wed, 10 Aug 2022 11:25:20 +0200 Subject: [PATCH 2/3] refactor(auth-socket-middleware): contemplate userId and give priority to userId --- src/auth-socket-middleware.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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(); }); }); From 5c7195d30415240aaf043d867502469d4ccdf2d6 Mon Sep 17 00:00:00 2001 From: Fabio Espinosa Date: Wed, 10 Aug 2022 11:25:54 +0200 Subject: [PATCH 3/3] refactor(handler): emit to both email and userId if provided --- src/handler.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/handler.ts b/src/handler.ts index d289957..4cc306c 100644 --- a/src/handler.ts +++ b/src/handler.ts @@ -29,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(); }); }