Skip to content

Commit 4f43a85

Browse files
chore: properly close websocket connection on device logout (#39383)
Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat>
1 parent 43d0cfc commit 4f43a85

4 files changed

Lines changed: 20 additions & 11 deletions

File tree

apps/meteor/client/views/root/hooks/loggedIn/useForceLogout.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useStream, useSessionDispatch } from '@rocket.chat/ui-contexts';
2-
import { Meteor } from 'meteor/meteor';
32
import { useEffect } from 'react';
43

54
export const useForceLogout = (userId: string) => {
@@ -9,13 +8,7 @@ export const useForceLogout = (userId: string) => {
98
useEffect(() => {
109
setForceLogout(false);
1110

12-
const unsubscribe = getNotifyUserStream(`${userId}/force_logout`, (sessionId) => {
13-
const currentSessionId = Meteor.connection._lastSessionId;
14-
15-
if (sessionId === currentSessionId) {
16-
window.location.reload();
17-
}
18-
11+
const unsubscribe = getNotifyUserStream(`${userId}/force_logout`, () => {
1912
setForceLogout(true);
2013
});
2114

apps/meteor/definition/externals/meteor/meteor.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ declare module 'meteor/meteor' {
4040
}
4141

4242
const server: {
43-
sessions: Map<string, { userId: string; heartbeat: DDPCommon.Heartbeat }>;
43+
sessions: Map<string, { userId: string; heartbeat: DDPCommon.Heartbeat; connectionHandle: Meteor.Connection }>;
4444
publish_handlers: {
4545
meteor_autoupdate_clientVersions(): void;
4646
};
@@ -118,7 +118,6 @@ declare module 'meteor/meteor' {
118118
},
119119
]
120120
): SubscriptionHandle;
121-
_lastSessionId: string;
122121

123122
call(methodName: string, ...args: [...unknown, callback?: (error: Error | null, result: unknown) => void]): void;
124123
}

apps/meteor/server/services/meteor/service.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ export class MeteorService extends ServiceClassInternal implements IMeteor {
3939

4040
new ListenersModule(this, notifications);
4141

42+
this.onEvent('user.forceLogout', (uid: string, sessionId?: string) => {
43+
if (sessionId) {
44+
const sessions = Meteor.server.sessions.get(sessionId);
45+
sessions?.connectionHandle.close();
46+
return;
47+
}
48+
Meteor.server.sessions.forEach((session) => {
49+
if (session.userId === uid) {
50+
session.connectionHandle.close();
51+
}
52+
});
53+
});
54+
4255
this.onEvent('watch.settings', async ({ clientAction, setting }): Promise<void> => {
4356
if (clientAction !== 'removed') {
4457
settings.set(setting);

ee/apps/ddp-streamer/src/DDPStreamer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ export class DDPStreamer extends ServiceClass {
5050
}
5151
});
5252

53-
this.onEvent('user.forceLogout', (uid: string) => {
53+
this.onEvent('user.forceLogout', (uid: string, sessionId?: string) => {
5454
this.wss?.clients.forEach((ws) => {
5555
const client = clientMap.get(ws);
56+
if (sessionId && client?.connection.id === sessionId) {
57+
ws.close();
58+
return;
59+
}
5660
if (client?.userId === uid) {
5761
ws.terminate();
5862
}

0 commit comments

Comments
 (0)