Skip to content

Commit

Permalink
Merge pull request #369 from boostcampwm2023/BE-feature/socket-connec…
Browse files Browse the repository at this point in the history
…tion

소켓 연결 시 인증 추가
  • Loading branch information
Conut-1 authored Dec 9, 2024
2 parents b8b8edc + 72cc59f commit d4b6ac8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
23 changes: 21 additions & 2 deletions nestjs-BE/server/src/board-trees/board-trees.gateway.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import {
OnGatewayConnection,
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions nestjs-BE/server/src/board-trees/board-trees.module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -9,6 +10,7 @@ import { BoardTree, BoardTreeSchema } from './schemas/board-tree.schema';
MongooseModule.forFeature([
{ name: BoardTree.name, schema: BoardTreeSchema },
]),
JwtModule,
],
providers: [BoardTreesService, BoardTreesGateway],
})
Expand Down

0 comments on commit d4b6ac8

Please sign in to comment.