From 646562894dd968cd52ff66e17e77b7a8887fe20a Mon Sep 17 00:00:00 2001 From: pagoru Date: Mon, 30 Sep 2024 02:06:04 +0200 Subject: [PATCH] fix: check server session when login/refresh and disconnect - fix #83 --- app/server/src/system/sessions.ts | 32 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/app/server/src/system/sessions.ts b/app/server/src/system/sessions.ts index cf13281..f79ae24 100644 --- a/app/server/src/system/sessions.ts +++ b/app/server/src/system/sessions.ts @@ -20,17 +20,26 @@ export const sessions = () => { const $checkSessions = async () => { const currentSessions = Object.keys(sessionMap); - const targetSessions = await getServerSessionList(); + const targetSessions = (await getServerSessionList()).map( + ({ key: [, accountId] }) => accountId, + ); console.log( `Checking sessions... (${currentSessions.length}..${targetSessions.length})`, ); + const toDeleteSessions = currentSessions.filter( + (accountId) => !targetSessions.includes(accountId), + ); + + //remove not active sessions + for (const accountId of toDeleteSessions) { + $disconnectFromLastServer(accountId, sessionMap[accountId].server); + delete sessionMap[accountId]; + } + const accountCheckList = [ - ...new Set([ - ...currentSessions, - ...targetSessions.map(({ key: [, accountId] }) => accountId), - ]), - ]; + ...new Set([...currentSessions, ...targetSessions]), + ].filter((accountId) => toDeleteSessions.includes(accountId)); //check accounts for (const accountId of accountCheckList) checkAccountSession(accountId); @@ -50,8 +59,6 @@ export const sessions = () => { "serverSessionByAccount", accountId, ]); - //check if is server claimed the session - const isFoundSessionClaimed = foundSession?.value?.claimed; const session = sessionMap[accountId]; console.warn( @@ -63,15 +70,6 @@ export const sessions = () => { "<<<<", ); - //if account has no active session or old session - if (!isFoundSessionClaimed && !session) return; - - //account is disconnected, disconnect from last server - if (!isFoundSessionClaimed) { - $disconnectFromLastServer(accountId, session.server); - return; - } - const currentSession = foundSession.value; //check if session server exists and changed if so disconnect from last server