From 0db1788ceff2a11646449154b48b463a2dabefbe Mon Sep 17 00:00:00 2001 From: kimminsu Date: Mon, 2 Dec 2024 17:19:33 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20feat:=20=EC=84=B8=EC=85=98=20?= =?UTF-8?q?=EC=A7=81=EB=A0=AC=ED=99=94=20=EB=B0=A9=EC=8B=9D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/src/auth/session/session.serializer.ts | 10 ++++++---- packages/backend/src/user/user.service.ts | 4 ++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/backend/src/auth/session/session.serializer.ts b/packages/backend/src/auth/session/session.serializer.ts index f81e5177..0fefa914 100644 --- a/packages/backend/src/auth/session/session.serializer.ts +++ b/packages/backend/src/auth/session/session.serializer.ts @@ -1,24 +1,26 @@ import { Injectable } from '@nestjs/common'; import { PassportSerializer } from '@nestjs/passport'; import { User } from '@/user/domain/user.entity'; +import { UserService } from '@/user/user.service'; @Injectable() export class SessionSerializer extends PassportSerializer { - constructor() { + constructor(private readonly userService: UserService) { super(); } async serializeUser( user: User, - done: (err: Error | null, user: User) => void, + done: (err: Error | null, userId: number) => void, ) { - done(null, user); + done(null, user.id); } async deserializeUser( - user: User, + userId: number, done: (err: Error | null, user: User | null) => void, ): Promise { + const user = await this.userService.findUserById(userId); return user ? done(null, user) : done(null, null); } } diff --git a/packages/backend/src/user/user.service.ts b/packages/backend/src/user/user.service.ts index ace5f7f7..d5c51eb8 100644 --- a/packages/backend/src/user/user.service.ts +++ b/packages/backend/src/user/user.service.ts @@ -70,6 +70,10 @@ export class UserService { }); } + async findUserById(id: number) { + return await this.dataSource.manager.findOne(User, { where: { id } }); + } + async getUserInfo(id: number) { const user = await this.dataSource.manager.findOne(User, { where: { id } }); if (!user) { From 8039fc92d7db8d94a272bd30adfc05aa1617dfc8 Mon Sep 17 00:00:00 2001 From: kimminsu Date: Mon, 2 Dec 2024 17:20:16 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9C=A8=20feat:=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=EB=90=9C=20=EC=A7=81=EB=A0=AC=ED=99=94=20=EB=B0=A9=EC=8B=9D=20?= =?UTF-8?q?=EC=9B=B9=EC=86=8C=EC=BC=93=EC=97=90=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/src/auth/session/webSocketSession.guard.ts | 10 ++++++++-- .../src/auth/session/websocketSession.service.ts | 10 ++++++++-- packages/backend/src/chat/chat.gateway.ts | 9 ++++++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/packages/backend/src/auth/session/webSocketSession.guard.ts b/packages/backend/src/auth/session/webSocketSession.guard.ts index f2c766a3..bb7cd891 100644 --- a/packages/backend/src/auth/session/webSocketSession.guard.ts +++ b/packages/backend/src/auth/session/webSocketSession.guard.ts @@ -10,25 +10,31 @@ import { Socket } from 'socket.io'; import { websocketCookieParse } from '@/auth/session/cookieParser'; import { MEMORY_STORE } from '@/auth/session.module'; import { User } from '@/user/domain/user.entity'; +import { UserService } from '@/user/user.service'; export interface SessionSocket extends Socket { session?: User; } export interface PassportSession extends SessionData { - passport: { user: User }; + passport: { user: number }; } @Injectable() export class WebSocketSessionGuard implements CanActivate { constructor( @Inject(MEMORY_STORE) private readonly sessionStore: MemoryStore, + private readonly userService: UserService, ) {} async canActivate(context: ExecutionContext): Promise { const socket: SessionSocket = context.switchToHttp().getRequest(); const cookieValue = websocketCookieParse(socket); const session = await this.getSession(cookieValue); - socket.session = session.passport.user; + const user = await this.userService.findUserById(session.passport.user); + if (!user) { + return false; + } + socket.session = user; return true; } diff --git a/packages/backend/src/auth/session/websocketSession.service.ts b/packages/backend/src/auth/session/websocketSession.service.ts index c6c248cc..ebaed768 100644 --- a/packages/backend/src/auth/session/websocketSession.service.ts +++ b/packages/backend/src/auth/session/websocketSession.service.ts @@ -2,15 +2,21 @@ import { MemoryStore } from 'express-session'; import { Socket } from 'socket.io'; import { websocketCookieParse } from '@/auth/session/cookieParser'; import { PassportSession } from '@/auth/session/webSocketSession.guard'; +import { UserService } from '@/user/user.service'; export class WebsocketSessionService { - constructor(private readonly sessionStore: MemoryStore) {} + constructor( + private readonly sessionStore: MemoryStore, + private readonly userService: UserService, + ) {} async getAuthenticatedUser(socket: Socket) { try { const cookieValue = websocketCookieParse(socket); const session = await this.getSession(cookieValue); - return session ? session.passport.user : null; + return session + ? await this.userService.findUserById(session.passport.user) + : null; // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (e) { return null; diff --git a/packages/backend/src/chat/chat.gateway.ts b/packages/backend/src/chat/chat.gateway.ts index 951b3365..78149333 100644 --- a/packages/backend/src/chat/chat.gateway.ts +++ b/packages/backend/src/chat/chat.gateway.ts @@ -45,10 +45,13 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { private readonly stockService: StockService, private readonly chatService: ChatService, private readonly mentionService: MentionService, - private readonly UserService: UserService, + private readonly userService: UserService, @Inject(MEMORY_STORE) sessionStore: MemoryStore, ) { - this.websocketSessionService = new WebsocketSessionService(sessionStore); + this.websocketSessionService = new WebsocketSessionService( + sessionStore, + userService, + ); } @UseGuards(WebSocketSessionGuard) @@ -169,7 +172,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { } private async searchMentionedUser(nickname: string, subName: string) { - return await this.UserService.searchOneUserByNicknameAndSubName( + return await this.userService.searchOneUserByNicknameAndSubName( nickname, subName, ); From 62b499d6be9311ce0909a05450a1f745a1d727da Mon Sep 17 00:00:00 2001 From: kimminsu Date: Mon, 2 Dec 2024 17:27:27 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=90=9B=20fix:=20=EC=A3=BC=EA=B0=80=20?= =?UTF-8?q?=EA=B2=80=EC=83=89=EC=97=90=EC=84=9C=20'%'=EA=B0=80=20=ED=8F=AC?= =?UTF-8?q?=ED=95=A8=EB=90=98=EC=96=B4=EB=8F=84=20=EA=B2=80=EC=83=89?= =?UTF-8?q?=EB=90=98=EB=8A=94=20=EB=AC=B8=EC=A0=9C=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/backend/src/stock/stock.service.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/backend/src/stock/stock.service.ts b/packages/backend/src/stock/stock.service.ts index 6f089af2..d38a142c 100644 --- a/packages/backend/src/stock/stock.service.ts +++ b/packages/backend/src/stock/stock.service.ts @@ -1,6 +1,6 @@ import { BadRequestException, Inject, Injectable } from '@nestjs/common'; import { plainToInstance } from 'class-transformer'; -import { DataSource, EntityManager } from 'typeorm'; +import { DataSource, EntityManager, Like } from 'typeorm'; import { Logger } from 'winston'; import { Stock } from './domain/stock.entity'; import { @@ -87,15 +87,13 @@ export class StockService { } async searchStock(stockName: string) { - const result = await this.datasource - .getRepository(Stock) - .createQueryBuilder('stock') - .where('stock.is_trading = :isTrading and stock.stock_name LIKE :name', { + const result = await this.datasource.manager.find(Stock, { + where: { isTrading: true, - name: `%${stockName}%`, - }) - .limit(10) - .getMany(); + name: Like(`%${stockName}%`), + }, + take: 10, + }); return new StockSearchResponse(result); }