mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix(api): use sessionId instead of cookie to close sockets
This commit is contained in:
parent
a92617db98
commit
ee23ef1f3e
@ -11,7 +11,6 @@ import {
|
|||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
Get,
|
Get,
|
||||||
Headers,
|
|
||||||
Inject,
|
Inject,
|
||||||
InternalServerErrorException,
|
InternalServerErrorException,
|
||||||
Param,
|
Param,
|
||||||
@ -25,7 +24,6 @@ import {
|
|||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||||
import { CsrfCheck, CsrfGen, CsrfGenAuth } from '@tekuconcept/nestjs-csrf';
|
import { CsrfCheck, CsrfGen, CsrfGenAuth } from '@tekuconcept/nestjs-csrf';
|
||||||
import cookie from 'cookie';
|
|
||||||
import { Request, Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
import { Session as ExpressSession } from 'express-session';
|
import { Session as ExpressSession } from 'express-session';
|
||||||
|
|
||||||
@ -73,13 +71,8 @@ export class BaseAuthController {
|
|||||||
logout(
|
logout(
|
||||||
@Session() session: ExpressSession,
|
@Session() session: ExpressSession,
|
||||||
@Res({ passthrough: true }) res: Response,
|
@Res({ passthrough: true }) res: Response,
|
||||||
@Headers() headers: Record<string, string>,
|
|
||||||
) {
|
) {
|
||||||
const parsedCookie = cookie.parse(headers['cookie']);
|
this.eventEmitter.emit('hook:user:logout', session);
|
||||||
const sessionCookie = encodeURIComponent(
|
|
||||||
String(parsedCookie[config.session.name] || ''),
|
|
||||||
);
|
|
||||||
this.eventEmitter.emit('hook:user:logout', sessionCookie);
|
|
||||||
res.clearCookie(config.session.name);
|
res.clearCookie(config.session.name);
|
||||||
|
|
||||||
session.destroy((error) => {
|
session.destroy((error) => {
|
||||||
|
|||||||
@ -20,7 +20,7 @@ import {
|
|||||||
import cookie from 'cookie';
|
import cookie from 'cookie';
|
||||||
import * as cookieParser from 'cookie-parser';
|
import * as cookieParser from 'cookie-parser';
|
||||||
import signature from 'cookie-signature';
|
import signature from 'cookie-signature';
|
||||||
import { SessionData } from 'express-session';
|
import { Session as ExpressSession, SessionData } from 'express-session';
|
||||||
import { Server, Socket } from 'socket.io';
|
import { Server, Socket } from 'socket.io';
|
||||||
import { sync as uid } from 'uid-safe';
|
import { sync as uid } from 'uid-safe';
|
||||||
|
|
||||||
@ -259,12 +259,10 @@ export class WebsocketGateway
|
|||||||
}
|
}
|
||||||
|
|
||||||
@OnEvent('hook:user:logout')
|
@OnEvent('hook:user:logout')
|
||||||
disconnectSockets(sessionCookie: string) {
|
disconnectSockets({ id }: ExpressSession) {
|
||||||
if (sessionCookie.length) {
|
for (const [, socket] of this.io.sockets.sockets) {
|
||||||
for (const [socketId, socket] of this.io.sockets.sockets) {
|
if (socket.data['sessionID'] === id) {
|
||||||
if (socket.handshake.headers.cookie?.includes(sessionCookie)) {
|
socket.disconnect(true);
|
||||||
this.io.sockets.sockets.get(socketId)?.disconnect(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
3
api/types/event-emitter.d.ts
vendored
3
api/types/event-emitter.d.ts
vendored
@ -6,6 +6,7 @@
|
|||||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { type Session as ExpressSession } from 'express-session';
|
||||||
import type { Document, Query } from 'mongoose';
|
import type { Document, Query } from 'mongoose';
|
||||||
import { type Socket } from 'socket.io';
|
import { type Socket } from 'socket.io';
|
||||||
|
|
||||||
@ -162,7 +163,7 @@ declare module '@nestjs/event-emitter' {
|
|||||||
model: TDefinition<Model>;
|
model: TDefinition<Model>;
|
||||||
permission: TDefinition<Permission>;
|
permission: TDefinition<Permission>;
|
||||||
role: TDefinition<Role>;
|
role: TDefinition<Role>;
|
||||||
user: TDefinition<User, { lastvisit: Subscriber; logout: string }>;
|
user: TDefinition<User, { lastvisit: Subscriber; logout: ExpressSession }>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* entities hooks having schemas */
|
/* entities hooks having schemas */
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user