feat: destroy session on logout user

This commit is contained in:
Sergey Yarkov 2022-01-30 20:04:06 +03:00
parent 448d071f93
commit 13d75468d9
2 changed files with 19 additions and 7 deletions

View File

@ -69,13 +69,10 @@ class WsService {
const { sessionId, userId, userName } = socket.data; const { sessionId, userId, userName } = socket.data;
const isExistSocketData = !!(sessionId && userId && userName); const isExistSocketData = !!(sessionId && userId && userName);
console.log('New socket connected');
/** /**
* Set user session and send online count * Set user session and send online count
*/ */
if (isExistSocketData) { if (isExistSocketData) {
console.log('Saving session');
this.sessionStore this.sessionStore
.saveSession(sessionId, { userId, userName, connected: true }) .saveSession(sessionId, { userId, userName, connected: true })
.then(() => this.sessionStore.getOnlineSessionsCount()) .then(() => this.sessionStore.getOnlineSessionsCount())
@ -93,8 +90,10 @@ class WsService {
/** /**
* Destroy session on user logout request * Destroy session on user logout request
*/ */
socket.on('user:logout', () => { socket.on('user:logout', async () => {
console.log('Destroy session', sessionId); if (isExistSocketData) {
await this.sessionStore.destroySession(sessionId);
}
}); });
/** /**

View File

@ -55,8 +55,6 @@ class RedisSessionStore {
return ['hmget', id.join('-'), 'userId', 'userName', 'connected']; return ['hmget', id.join('-'), 'userId', 'userName', 'connected'];
}); });
console.log(commands);
return this.redisClient return this.redisClient
.multi(commands) .multi(commands)
.exec() .exec()
@ -90,6 +88,21 @@ class RedisSessionStore {
.expire(id, SESSION_TTL) .expire(id, SESSION_TTL)
.exec(); .exec();
} }
/**
* Remove session from redis store
*
* @param id Session id
*/
public async destroySession(id: string): Promise<number> {
const session = await this.findSession(id);
if (session) {
return this.redisClient.del(id);
}
return 0;
}
} }
export default RedisSessionStore; export default RedisSessionStore;