mirror of
https://github.com/sergeyyarkov/educt-server.git
synced 2026-10-11 10:19:20 +03:00
feat: destroy session on logout user
This commit is contained in:
parent
448d071f93
commit
13d75468d9
@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user