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], })