Skip to content

Commit df7c4e2

Browse files
authored
Merge pull request #222 from boostcampwm-2024/feat/#221-refactor-user-decorator
♻️ refactor: 유저 주입 데코레이터 최적화
2 parents 74e1887 + a3abb55 commit df7c4e2

File tree

4 files changed

+10
-22
lines changed

4 files changed

+10
-22
lines changed

back/src/auth/service/auth.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ export class AuthService {
1212
this.redis = this.redisService.getOrThrow();
1313
}
1414

15-
async getUserIdFromSession(sid: string): Promise<number | null> {
15+
async getUserIdFromSession(sid: string): Promise<[number, string] | null> {
1616
const session = JSON.parse(await this.redis.get(`user:${sid}`));
1717
if (!session) return null;
1818
const userId = session.id;
19-
if (!userId) return null;
20-
return userId;
19+
const userLoginId = session.loginId;
20+
if (!userId || !userLoginId) return null;
21+
return [userId, userLoginId];
2122
}
2223

2324
async setUserStatusLogin(sid: string) {

back/src/domains/user/service/user.service.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ export class UserService {
3434
this.redis = this.redisService.getOrThrow();
3535
}
3636

37-
async getUser(userId: number) {
38-
return await this.userRepository.findById(userId);
39-
}
40-
4137
async registerUser(userCreateDto: UserCreateDto, role: string = USER_ROLE.USER) {
4238
if (await this.userRepository.findByLoginId(userCreateDto.loginId)) {
4339
throw new ConflictException('이미 존재하는 사용자입니다.');
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
import { Injectable } from '@nestjs/common';
22

33
import { AuthService } from 'src/auth/service/auth.service';
4-
import { User } from 'src/domains/user/entity/user.entity';
5-
import { UserService } from 'src/domains/user/service/user.service';
64

75
import { UserParamDto } from './userParamDto';
86

97
@Injectable()
108
export class UserDecoratorService {
11-
constructor(
12-
private readonly userService: UserService,
13-
private readonly authService: AuthService,
14-
) {}
9+
constructor(private readonly authService: AuthService) {}
1510

1611
async getUserParam(ctx: any) {
1712
const req = ctx.switchToHttp().getRequest();
1813
const sid = req.cookies['SID'];
19-
const userId = await this.authService.getUserIdFromSession(sid);
20-
21-
if (!userId) return null;
22-
23-
const user: User = await this.userService.getUser(userId);
24-
return new UserParamDto(user);
14+
if (!sid) return;
15+
const [userId, userLoginId] = await this.authService.getUserIdFromSession(sid);
16+
if (!userId || !userLoginId) return null;
17+
return new UserParamDto({ id: userId, loginId: userLoginId });
2518
}
2619
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
export class UserParamDto {
2-
constructor({ id, loginId, loginPassword }) {
2+
constructor({ id, loginId }) {
33
this.id = id;
44
this.loginId = loginId;
5-
this.loginPassword = loginPassword;
65
}
76

87
id: number;
98
loginId: string;
10-
loginPassword: string;
119
}

0 commit comments

Comments
 (0)