From 72cc59fd52bb7429293be68303fd743f9f60d5b4 Mon Sep 17 00:00:00 2001 From: Conut-1 <1mim1@naver.com> Date: Fri, 6 Dec 2024 21:14:15 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=97=B0=EA=B2=B0=20=EC=8B=9C=20?= =?UTF-8?q?=EC=9D=B8=EC=A6=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/board-trees/board-trees.gateway.ts | 23 +++++++++++++++++-- .../src/board-trees/board-trees.module.ts | 2 ++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/nestjs-BE/server/src/board-trees/board-trees.gateway.ts b/nestjs-BE/server/src/board-trees/board-trees.gateway.ts index f1e8e110..d54c2e45 100644 --- a/nestjs-BE/server/src/board-trees/board-trees.gateway.ts +++ b/nestjs-BE/server/src/board-trees/board-trees.gateway.ts @@ -1,4 +1,7 @@ +import { UnauthorizedException } from '@nestjs/common'; +import { JwtService } from '@nestjs/jwt'; import { + OnGatewayConnection, SubscribeMessage, WebSocketGateway, WebSocketServer, @@ -13,12 +16,28 @@ import { } from '../crdt/operation'; @WebSocketGateway({ namespace: 'board' }) -export class BoardTreesGateway { - constructor(private boardTreesService: BoardTreesService) {} +export class BoardTreesGateway implements OnGatewayConnection { + constructor( + private boardTreesService: BoardTreesService, + private jwtService: JwtService, + ) {} @WebSocketServer() server: Server; + handleConnection(client: Socket, token: string) { + if (!token) { + client.disconnect(); + throw new UnauthorizedException(); + } + try { + this.jwtService.verify(token); + } catch (error) { + client.disconnect(); + throw new UnauthorizedException(); + } + } + @SubscribeMessage('joinBoard') async handleJoinBoard(client: Socket, payload: string) { const payloadObject = JSON.parse(payload); diff --git a/nestjs-BE/server/src/board-trees/board-trees.module.ts b/nestjs-BE/server/src/board-trees/board-trees.module.ts index ea4d9b41..211084d2 100644 --- a/nestjs-BE/server/src/board-trees/board-trees.module.ts +++ b/nestjs-BE/server/src/board-trees/board-trees.module.ts @@ -1,4 +1,5 @@ import { Module } from '@nestjs/common'; +import { JwtModule } from '@nestjs/jwt'; import { MongooseModule } from '@nestjs/mongoose'; import { BoardTreesService } from './board-trees.service'; import { BoardTreesGateway } from './board-trees.gateway'; @@ -9,6 +10,7 @@ import { BoardTree, BoardTreeSchema } from './schemas/board-tree.schema'; MongooseModule.forFeature([ { name: BoardTree.name, schema: BoardTreeSchema }, ]), + JwtModule, ], providers: [BoardTreesService, BoardTreesGateway], })