From c42cd578b5e1be866f8454bb1243f4f9becc5ade Mon Sep 17 00:00:00 2001 From: J-hoplin1 Date: Sat, 29 Jun 2024 21:16:11 +0900 Subject: [PATCH 1/4] =?UTF-8?q?Refactor:=20User=EB=AA=A8=EB=93=88=20?= =?UTF-8?q?=EB=A0=88=ED=8F=AC=EC=A7=80=ED=86=A0=EB=A6=AC=20=EB=A0=88?= =?UTF-8?q?=EC=9D=B4=EC=96=B4=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/users/users.module.ts | 3 ++- src/modules/users/users.repository.ts | 32 +++++++++++++++++++++++++++ src/modules/users/users.service.ts | 28 ++++++----------------- 3 files changed, 41 insertions(+), 22 deletions(-) create mode 100644 src/modules/users/users.repository.ts diff --git a/src/modules/users/users.module.ts b/src/modules/users/users.module.ts index 616cbcd..e6fcaa3 100644 --- a/src/modules/users/users.module.ts +++ b/src/modules/users/users.module.ts @@ -5,6 +5,7 @@ import { MongooseModule } from '@nestjs/mongoose'; import { JwtModule } from '@nestjs/jwt'; import { JwtStrategy } from './guards/strategy'; import { User, UserSchema } from '@src/infrastructure'; +import { UsersRepository } from './users.repository'; @Module({ imports: [ @@ -14,6 +15,6 @@ import { User, UserSchema } from '@src/infrastructure'; }), ], controllers: [UsersController], - providers: [UsersService, JwtStrategy], + providers: [UsersService, UsersRepository, JwtStrategy], }) export class UsersModule {} diff --git a/src/modules/users/users.repository.ts b/src/modules/users/users.repository.ts new file mode 100644 index 0000000..0541b40 --- /dev/null +++ b/src/modules/users/users.repository.ts @@ -0,0 +1,32 @@ +import { Injectable } from '@nestjs/common'; +import { InjectModel } from '@nestjs/mongoose'; +import { User } from '@src/infrastructure'; +import { Model } from 'mongoose'; + +@Injectable() +export class UsersRepository { + constructor( + @InjectModel(User.name) private readonly userModel: Model, + ) {} + + async createUser(deviceToken: string): Promise { + const user = await this.userModel + .findOneAndUpdate( + { + deviceToken: deviceToken, + }, + { + $set: { + deviceToken: deviceToken, + }, + }, + { + new: true, + upsert: true, + }, + ) + .lean(); + const userId = user._id.toString(); + return userId; + } +} diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index 57eb39f..022bcfd 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -1,40 +1,26 @@ import { Injectable } from '@nestjs/common'; -import { InjectModel } from '@nestjs/mongoose'; -import { Model } from 'mongoose'; import { CreateUserDto } from './dto'; import { JwtService } from '@nestjs/jwt'; import { JwtPayload } from 'src/common/types/type'; import { ConfigService } from '@nestjs/config'; -import { User } from '@src/infrastructure'; +import { UsersRepository } from './users.repository'; @Injectable() export class UsersService { constructor( - @InjectModel(User.name) private readonly userModel: Model, + private readonly userRepository: UsersRepository, private readonly jwt: JwtService, private readonly config: ConfigService, ) {} async createUser(dto: CreateUserDto): Promise { - const user = await this.userModel - .findOneAndUpdate( - { - deviceToken: dto.deviceToken, - }, - { - $set: { - deviceToken: dto.deviceToken, - }, - }, - { - new: true, - upsert: true, - }, - ) - .lean(); + // 새로운 user의 ID + const userId = await this.userRepository.createUser(dto.deviceToken); + // JWT Token Payload const tokenPayload: JwtPayload = { - id: user._id.toString(), + id: userId, }; + // JWT Token 발급 const token = await this.issueAccessToken(tokenPayload); return token; } From 2afe9a038b704127ced2bab7b5d4664b54677b8a Mon Sep 17 00:00:00 2001 From: J-hoplin1 Date: Sun, 30 Jun 2024 00:10:20 +0900 Subject: [PATCH 2/4] =?UTF-8?q?Refactor:=20User=20Repository=20createUser?= =?UTF-8?q?=20=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=9D=B4=EB=A6=84=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD(findOrCreateUser)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/users/users.repository.ts | 2 +- src/modules/users/users.service.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/users/users.repository.ts b/src/modules/users/users.repository.ts index 0541b40..421bbd9 100644 --- a/src/modules/users/users.repository.ts +++ b/src/modules/users/users.repository.ts @@ -9,7 +9,7 @@ export class UsersRepository { @InjectModel(User.name) private readonly userModel: Model, ) {} - async createUser(deviceToken: string): Promise { + async findOrCreateUser(deviceToken: string): Promise { const user = await this.userModel .findOneAndUpdate( { diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index 022bcfd..0ff8072 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -15,7 +15,7 @@ export class UsersService { async createUser(dto: CreateUserDto): Promise { // 새로운 user의 ID - const userId = await this.userRepository.createUser(dto.deviceToken); + const userId = await this.userRepository.findOrCreateUser(dto.deviceToken); // JWT Token Payload const tokenPayload: JwtPayload = { id: userId, From f87083a9b5144e045d8585902dc6e8f4673f0e1f Mon Sep 17 00:00:00 2001 From: J-hoplin1 Date: Sun, 30 Jun 2024 00:24:55 +0900 Subject: [PATCH 3/4] =?UTF-8?q?Refactor:=20User=20Module=EC=9D=98=20issueA?= =?UTF-8?q?ccessToken=20Auth=20Module=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.module.ts | 2 ++ src/modules/auth/auth.module.ts | 14 ++++++++++++++ src/modules/auth/auth.service.ts | 20 ++++++++++++++++++++ src/modules/users/users.module.ts | 6 ++---- src/modules/users/users.service.ts | 16 +++------------- 5 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 src/modules/auth/auth.module.ts create mode 100644 src/modules/auth/auth.service.ts diff --git a/src/app.module.ts b/src/app.module.ts index 192f089..258ea62 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -7,6 +7,7 @@ import { AppService } from './app.service'; import { ConfigModule } from '@nestjs/config'; import { DatabaseModule } from '@src/infrastructure'; import { UsersModule } from './modules/users/users.module'; +import { AuthModule } from './modules/auth/auth.module'; @Module({ imports: [ @@ -17,6 +18,7 @@ import { UsersModule } from './modules/users/users.module'; }), DatabaseModule, UsersModule, + AuthModule, ], controllers: [AppController], providers: [AppService], diff --git a/src/modules/auth/auth.module.ts b/src/modules/auth/auth.module.ts new file mode 100644 index 0000000..0980edc --- /dev/null +++ b/src/modules/auth/auth.module.ts @@ -0,0 +1,14 @@ +import { Module } from '@nestjs/common'; +import { AuthService } from './auth.service'; +import { JwtModule } from '@nestjs/jwt'; + +@Module({ + imports: [ + JwtModule.register({ + global: true, + }), + ], + providers: [AuthService], + exports: [AuthService], +}) +export class AuthModule {} diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts new file mode 100644 index 0000000..5323c53 --- /dev/null +++ b/src/modules/auth/auth.service.ts @@ -0,0 +1,20 @@ +import { Injectable } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { JwtService } from '@nestjs/jwt'; +import { JwtPayload } from '@src/common/types/type'; + +@Injectable() +export class AuthService { + constructor( + private readonly jwt: JwtService, + private readonly config: ConfigService, + ) {} + + async issueAccessToken(payload: JwtPayload): Promise { + const token = await this.jwt.signAsync(payload, { + secret: this.config.get('JWT_SECRET'), + expiresIn: this.config.get('JWT_EXPIRE_TIME'), + }); + return token; + } +} diff --git a/src/modules/users/users.module.ts b/src/modules/users/users.module.ts index e6fcaa3..1e85fd2 100644 --- a/src/modules/users/users.module.ts +++ b/src/modules/users/users.module.ts @@ -2,17 +2,15 @@ import { Module } from '@nestjs/common'; import { UsersController } from './users.controller'; import { UsersService } from './users.service'; import { MongooseModule } from '@nestjs/mongoose'; -import { JwtModule } from '@nestjs/jwt'; import { JwtStrategy } from './guards/strategy'; import { User, UserSchema } from '@src/infrastructure'; import { UsersRepository } from './users.repository'; +import { AuthModule } from '../auth/auth.module'; @Module({ imports: [ + AuthModule, MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]), - JwtModule.register({ - global: true, - }), ], controllers: [UsersController], providers: [UsersService, UsersRepository, JwtStrategy], diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index 0ff8072..65bbaad 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -1,16 +1,14 @@ import { Injectable } from '@nestjs/common'; import { CreateUserDto } from './dto'; -import { JwtService } from '@nestjs/jwt'; import { JwtPayload } from 'src/common/types/type'; -import { ConfigService } from '@nestjs/config'; import { UsersRepository } from './users.repository'; +import { AuthService } from '../auth/auth.service'; @Injectable() export class UsersService { constructor( private readonly userRepository: UsersRepository, - private readonly jwt: JwtService, - private readonly config: ConfigService, + private readonly authService: AuthService, ) {} async createUser(dto: CreateUserDto): Promise { @@ -21,15 +19,7 @@ export class UsersService { id: userId, }; // JWT Token 발급 - const token = await this.issueAccessToken(tokenPayload); - return token; - } - - private async issueAccessToken(payload: JwtPayload) { - const token = await this.jwt.signAsync(payload, { - secret: this.config.get('JWT_SECRET'), - expiresIn: this.config.get('JWT_EXPIRE_TIME'), - }); + const token = await this.authService.issueAccessToken(tokenPayload); return token; } } From 46244f4e8cbfa754022ca3e4febaa9bd5618f70e Mon Sep 17 00:00:00 2001 From: J-hoplin1 Date: Sun, 30 Jun 2024 01:09:29 +0900 Subject: [PATCH 4/4] =?UTF-8?q?Refactor:=20User=20Repository=EC=9D=98=20?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=EB=AA=85=20=EB=B3=80=EA=B2=BD,=20?= =?UTF-8?q?=EC=BF=BC=EB=A6=AC=20=EB=8F=99=EC=9E=91=20=EB=B3=80=EA=B2=BD(?= =?UTF-8?q?=EB=B6=84=EB=A6=AC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/users/users.repository.ts | 28 +++++++++++---------------- src/modules/users/users.service.ts | 4 ++-- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/modules/users/users.repository.ts b/src/modules/users/users.repository.ts index 421bbd9..dbc68f1 100644 --- a/src/modules/users/users.repository.ts +++ b/src/modules/users/users.repository.ts @@ -9,24 +9,18 @@ export class UsersRepository { @InjectModel(User.name) private readonly userModel: Model, ) {} - async findOrCreateUser(deviceToken: string): Promise { + async findOrCreate(deviceToken: string) { const user = await this.userModel - .findOneAndUpdate( - { - deviceToken: deviceToken, - }, - { - $set: { - deviceToken: deviceToken, - }, - }, - { - new: true, - upsert: true, - }, - ) + .findOne({ + deviceToken: deviceToken, + }) .lean(); - const userId = user._id.toString(); - return userId; + if (user) { + return user; + } + const newUser = await this.userModel.create({ + deviceToken: deviceToken, + }); + return newUser; } } diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index 65bbaad..11e0e61 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -13,10 +13,10 @@ export class UsersService { async createUser(dto: CreateUserDto): Promise { // 새로운 user의 ID - const userId = await this.userRepository.findOrCreateUser(dto.deviceToken); + const user = await this.userRepository.findOrCreate(dto.deviceToken); // JWT Token Payload const tokenPayload: JwtPayload = { - id: userId, + id: user._id.toString(), }; // JWT Token 발급 const token = await this.authService.issueAccessToken(tokenPayload);