From 13d75468d995e2ee31a63873533d348c4ee2e557 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Sun, 30 Jan 2022 20:04:06 +0300 Subject: [PATCH] feat: destroy session on logout user --- app/Services/WsService.ts | 9 ++++----- app/Store/SessionStore.ts | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/Services/WsService.ts b/app/Services/WsService.ts index e9ae8db..33b923e 100644 --- a/app/Services/WsService.ts +++ b/app/Services/WsService.ts @@ -69,13 +69,10 @@ class WsService { const { sessionId, userId, userName } = socket.data; const isExistSocketData = !!(sessionId && userId && userName); - console.log('New socket connected'); - /** * Set user session and send online count */ if (isExistSocketData) { - console.log('Saving session'); this.sessionStore .saveSession(sessionId, { userId, userName, connected: true }) .then(() => this.sessionStore.getOnlineSessionsCount()) @@ -93,8 +90,10 @@ class WsService { /** * Destroy session on user logout request */ - socket.on('user:logout', () => { - console.log('Destroy session', sessionId); + socket.on('user:logout', async () => { + if (isExistSocketData) { + await this.sessionStore.destroySession(sessionId); + } }); /** diff --git a/app/Store/SessionStore.ts b/app/Store/SessionStore.ts index f8b90a8..12e43c6 100644 --- a/app/Store/SessionStore.ts +++ b/app/Store/SessionStore.ts @@ -55,8 +55,6 @@ class RedisSessionStore { return ['hmget', id.join('-'), 'userId', 'userName', 'connected']; }); - console.log(commands); - return this.redisClient .multi(commands) .exec() @@ -90,6 +88,21 @@ class RedisSessionStore { .expire(id, SESSION_TTL) .exec(); } + + /** + * Remove session from redis store + * + * @param id Session id + */ + public async destroySession(id: string): Promise { + const session = await this.findSession(id); + + if (session) { + return this.redisClient.del(id); + } + + return 0; + } } export default RedisSessionStore;