Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

소켓 연결 시 인증 추가 #369

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading