Skip to content

Commit

Permalink
Bug/#326 - 세션 직렬화 방식 변경 (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
xjfcnfw3 authored Dec 2, 2024

Unverified

This user has not yet uploaded their public signing key.
2 parents 068bc2e + 62b499d commit 2bf0dc9
Showing 6 changed files with 39 additions and 20 deletions.
10 changes: 6 additions & 4 deletions packages/backend/src/auth/session/session.serializer.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
const user = await this.userService.findUserById(userId);
return user ? done(null, user) : done(null, null);
}
}
10 changes: 8 additions & 2 deletions packages/backend/src/auth/session/webSocketSession.guard.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
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;
}

10 changes: 8 additions & 2 deletions packages/backend/src/auth/session/websocketSession.service.ts
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 6 additions & 3 deletions packages/backend/src/chat/chat.gateway.ts
Original file line number Diff line number Diff line change
@@ -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,
);
16 changes: 7 additions & 9 deletions packages/backend/src/stock/stock.service.ts
Original file line number Diff line number Diff line change
@@ -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);
}

4 changes: 4 additions & 0 deletions packages/backend/src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -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) {

0 comments on commit 2bf0dc9

Please sign in to comment.