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 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);
}
});
/**

View File

@ -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<number> {
const session = await this.findSession(id);
if (session) {
return this.redisClient.del(id);
}
return 0;
}
}
export default RedisSessionStore;