From 6fc4a0fe391fd12571e275bb8f0808e332a2f82f Mon Sep 17 00:00:00 2001 From: geonhee-min Date: Tue, 16 Dec 2025 17:26:36 +0900 Subject: [PATCH] =?UTF-8?q?issue=20#=20-=20DTO=20=ED=8C=A8=ED=82=A4?= =?UTF-8?q?=EC=A7=80=20=EB=A0=88=EC=A7=80=EC=8A=A4=ED=8A=B8=EB=A6=AC=20?= =?UTF-8?q?=EC=A0=84=ED=99=98=20=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/main.ts | 11 ++ src/modules/account/account.controller.ts | 32 ++-- src/modules/account/account.service.ts | 73 +++++---- yarn.lock | 179 +++++++++++++++++++++- 5 files changed, 240 insertions(+), 57 deletions(-) diff --git a/package.json b/package.json index 031282e..4621e94 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "drizzle-pull:prod": "dotenv -e .env.prod -- drizzle-kit pull" }, "dependencies": { - "@baekyangdan/core-utils": "^1.0.9", + "@baekyangdan/core-utils": "^1.0.21", "@fastify/cookie": "^11.0.2", "@nestjs/class-transformer": "^0.4.0", "@nestjs/class-validator": "^0.13.4", diff --git a/src/main.ts b/src/main.ts index e5ad215..333b895 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,6 +8,7 @@ import { AllExceptionsFilter } from './common/filters/all-exceptions.filter'; import fastifyCookie from '@fastify/cookie'; import * as path from 'path'; import * as fs from 'fs'; +import { ValidationPipe } from '@nestjs/common'; async function bootstrap() { const isProd = process.env.NODE_ENV === 'prod'; @@ -23,6 +24,16 @@ async function bootstrap() { AppModule, new FastifyAdapter(!isProd ? { https: httpsOptions } : undefined) ); + app.useGlobalPipes( + new ValidationPipe({ + transform: true, + whitelist: true, + forbidNonWhitelisted: true, + transformOptions: { + enableImplicitConversion: true + } + }) + ) app.enableCors({ origin: (origin, callback) => { // origin이 없는 경우(local file, curl 등) 허용 diff --git a/src/modules/account/account.controller.ts b/src/modules/account/account.controller.ts index 32ecb33..19e2ebc 100644 --- a/src/modules/account/account.controller.ts +++ b/src/modules/account/account.controller.ts @@ -1,6 +1,6 @@ import { Body, Controller, Get, Headers, Post, Query, Req, Res, UseGuards } from "@nestjs/common"; import { AccountService } from "./account.service"; -import * as DTO from "./dto"; +import { SchedulerDTO as DTO } from "@baekyangdan/core-utils"; import { Public } from "src/common/decorators/public.decorator"; import type { FastifyReply, FastifyRequest } from "fastify"; import { AuthGuard } from "@nestjs/passport"; @@ -39,16 +39,16 @@ export class AccountController { } @Public() - @Post(AccountApi.sendResetPasswordCode) - async sendResetPasswordCode(@Body() body: DTO.SendResetPasswordCodeRequest): Promise { - const result = await this.accountService.sendResetPasswordCode(body); + @Post(AccountApi.sendPasswordResetCode) + async sendPasswordResetCode(@Body() body: DTO.SendPasswordResetCodeRequest): Promise { + const result = await this.accountService.sendPasswordResetCode(body); return result; } @Public() - @Post(AccountApi.verifyResetPasswordCode) - async verifyResetPasswordCode(@Body() body: DTO.VerifyResetPasswordCodeRequest): Promise { - const result = await this.accountService.verifyResetPasswordCode(body); + @Post(AccountApi.verifyPasswordResetCode) + async verifyPasswordResetCode(@Body() body: DTO.VerifyPasswordResetCodeRequest): Promise { + const result = await this.accountService.verifyPasswordResetCode(body); return result; } @@ -71,19 +71,14 @@ export class AccountController { async login(@Body() body: DTO.LoginRequest, @Res({ passthrough: true }) res: FastifyReply): Promise { const result = await this.accountService.login(body); if (result.success) { - res.setCookie('refresh_token', result.refreshToken!, { + res.setCookie('refresh_token', result.data.refreshToken!, { httpOnly: true, path: '/', secure: true, maxAge: 7 * 24 * 60 * 60 * 1000 }); } - return { - success: result.success, - message: result.message, - error: result.error, - accessToken: result.accessToken - }; + return result; } @Public() @@ -92,19 +87,14 @@ export class AccountController { async refreshAccessToken(@Req() req, @Res({ passthrough: true }) res: FastifyReply): Promise { const result = await this.accountService.refreshAccessToken(req.user.id); if (result.success) { - res.setCookie('refresh_token', result.refreshToken!, { + res.setCookie('refresh_token', result.data.refreshToken!, { httpOnly: true, path: '/', secure: true, maxAge: 7 * 24 * 60 * 60 * 1000 }); - return { - success: result.success, - message: result.message, - error: result.error, - accessToken: result.accessToken - } + return result; } return result; } diff --git a/src/modules/account/account.service.ts b/src/modules/account/account.service.ts index c07dff8..f7e14ee 100644 --- a/src/modules/account/account.service.ts +++ b/src/modules/account/account.service.ts @@ -1,6 +1,6 @@ import { Inject, Injectable } from "@nestjs/common"; import { AccountRepo } from "./account.repo"; -import * as DTO from './dto'; +import { SchedulerDTO as DTO } from '@baekyangdan/core-utils'; import { MailerService } from "src/util/mailer/mailer.service"; import { Generator } from "src/util/generator"; import Redis from "ioredis"; @@ -20,7 +20,7 @@ export class AccountService { const { type, value } = data; const count = await this.accountRepo.checkIdExists(type, value); - return { isDuplicated: count > 0, success: true }; + return { success: true, message: '중복 체크 완료', data: { isDuplicated: count > 0 }}; } async sendVerificationCode(data: DTO.SendEmailVerificationCodeRequest): Promise { @@ -30,11 +30,11 @@ export class AccountService { const result = await this.mailerService.sendMail(email, " 이메일 인증 코드", html); if (result.rejected.length > 0) { - return { success: false, error: result.response } + return { success: false, error: result.response, code: '' } } else { await this.redis.set(`verify:${email}`, code, 'EX', 600); - return { success: true, message: "이메일 발송 완료" }; + return { success: true, message: "이메일 발송 완료", data: {} }; } } @@ -44,14 +44,14 @@ export class AccountService { const storedCode = await this.redis.get(`verify:${email}`); if (!storedCode) { - return { verified: false, success: true, error: '잘못된 이메일이거나 코드가 만료되었습니다.'}; + return { success: false, error: '잘못된 이메일이거나 코드가 만료되었습니다.', code: ''}; } if (storedCode !== code) { - return { verified: false, success: true, error: "잘못된 코드입니다." }; + return { success: true, message: "잘못된 코드입니다.", data: { verified: false } }; } await this.redis.del(`verify:${email}`); - return { verified: true, success: true, message: "이메일 인증이 완료되었습니다." }; + return { success: true, message: "이메일 인증이 완료되었습니다.", data: { verified: true } }; } async signup(data: DTO.SignupRequest): Promise { @@ -62,12 +62,14 @@ export class AccountService { if (result.rowCount) { return { success: true, - message: "회원가입이 완료되었습니다." + message: "회원가입이 완료되었습니다.", + data: {} }; } else { return { success: false, - error: "회원가입에 실패하였습니다." + error: "회원가입에 실패하였습니다.", + code: '' }; } @@ -81,7 +83,8 @@ export class AccountService { if (!queryResult || (queryResult.length < 1)) { return { success: false, - message: `존재하지 않는 ${typeValue} 입니다.` + error: `존재하지 않는 ${typeValue} 입니다.`, + code: '' }; } @@ -90,7 +93,8 @@ export class AccountService { if (!isPasswordMatch) { return { success: false, - message: `비밀번호가 맞지 않습니다.` + error: `비밀번호가 맞지 않습니다.`, + code: '' }; } @@ -101,8 +105,11 @@ export class AccountService { return { success: true, - accessToken: accessToken, - refreshToken: refreshToken + data: { + accessToken: accessToken, + refreshToken: refreshToken + }, + message: '로그인 성공' }; } } @@ -110,13 +117,16 @@ export class AccountService { async refreshAccessToken(id: string): Promise { const { accessToken, refreshToken } = this.authService.refreshTokens(id); return { - accessToken: accessToken, - refreshToken: refreshToken, - success: true + success: true, + message: '토큰 갱신 완료', + data: { + accessToken: accessToken, + refreshToken: refreshToken + } }; } - async sendResetPasswordCode(data: DTO.SendResetPasswordCodeRequest): Promise { + async sendPasswordResetCode(data: DTO.SendPasswordResetCodeRequest): Promise { const { email } = data; const count = await this.accountRepo.checkIdExists('email', email); @@ -124,7 +134,8 @@ export class AccountService { if (count === 0) { return { success: false, - error: "찾을 수 없는 사용자" + error: "찾을 수 없는 사용자", + code: '' }; } @@ -138,7 +149,8 @@ export class AccountService { if (result.rejected.length > 0) { return { success: false, - error: result.response + error: result.response, + code: '' }; } @@ -146,11 +158,12 @@ export class AccountService { return { success: true, - message: "비밀번호 초기화 코드 발송 완료" + message: "비밀번호 초기화 코드 발송 완료", + data: {} }; } - async verifyResetPasswordCode(data: DTO.VerifyResetPasswordCodeRequest): Promise { + async verifyPasswordResetCode(data: DTO.VerifyPasswordResetCodeRequest): Promise { const { email, code } = data; const storedCode = await this.redis.get(`resetPassword:${email}`); @@ -158,16 +171,16 @@ export class AccountService { if (!storedCode) { return { success: false, - verified: false, - error: "잘못된 이메일이거나 코드가 만료되었습니다." + error: "잘못된 이메일이거나 코드가 만료되었습니다.", + code: '' }; } if (storedCode !== code) { return { success: false, - verified: false, - error: "잘못된 코드입니다." + error: "잘못된 코드입니다.", + code: '' }; } @@ -175,8 +188,8 @@ export class AccountService { return { success: true, - verified: true, - message: "비밀번호 초기화 코드 인증 완료" + message: "비밀번호 초기화 코드 인증 완료", + data: { verified: true } }; } @@ -188,13 +201,15 @@ export class AccountService { if (!result.rowCount || result.rowCount === 0) { return { success: false, - error: "비밀번호 초기화 실패" + error: "비밀번호 초기화 실패", + code: '' }; } return { success: true, - message: "비밀번호 초기화 성공" + message: "비밀번호 초기화 성공", + data: {} }; } } \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 3768bfe..1641b89 100644 --- a/yarn.lock +++ b/yarn.lock @@ -999,14 +999,17 @@ __metadata: languageName: node linkType: hard -"@baekyangdan/core-utils@npm:^1.0.9": - version: 1.0.9 - resolution: "@baekyangdan/core-utils@npm:1.0.9::__archiveUrl=https%3A%2F%2Fgitea.bkdhome.p-e.kr%2Fapi%2Fpackages%2Fbaekyangdan%2Fnpm%2F%2540baekyangdan%252Fcore-utils%2F-%2F1.0.9%2Fcore-utils-1.0.9.tgz" +"@baekyangdan/core-utils@npm:^1.0.21": + version: 1.0.21 + resolution: "@baekyangdan/core-utils@npm:1.0.21::__archiveUrl=https%3A%2F%2Fgitea.bkdhome.p-e.kr%2Fapi%2Fpackages%2Fbaekyangdan%2Fnpm%2F%2540baekyangdan%252Fcore-utils%2F-%2F1.0.21%2Fcore-utils-1.0.21.tgz" dependencies: + "@swc/core": "npm:^1.15.5" + class-transformer: "npm:^0.5.1" + class-validator: "npm:^0.14.3" date-fns: "npm:^4.1.0" reflect-metadata: "npm:^0.2.2" tsup: "npm:^8.5.1" - checksum: 10c0/76c23a35dcc40856cd1be0b632a71ddbdb1740b397ddfcf4e2d8b307d0c127419382c0f89a0d92fc2546e9a3e313172d825942d78121d2d16cfde43a8729a7ca + checksum: 10c0/52a3e70312ffdad0163f7c6954a8fa126583035500f505bacbd52045484ed9783a522f96661ab0572c0578cc97589fc767b877b9de1a06dad2fa537a79f778ef languageName: node linkType: hard @@ -3499,6 +3502,138 @@ __metadata: languageName: node linkType: hard +"@swc/core-darwin-arm64@npm:1.15.5": + version: 1.15.5 + resolution: "@swc/core-darwin-arm64@npm:1.15.5" + conditions: os=darwin & cpu=arm64 + languageName: node + linkType: hard + +"@swc/core-darwin-x64@npm:1.15.5": + version: 1.15.5 + resolution: "@swc/core-darwin-x64@npm:1.15.5" + conditions: os=darwin & cpu=x64 + languageName: node + linkType: hard + +"@swc/core-linux-arm-gnueabihf@npm:1.15.5": + version: 1.15.5 + resolution: "@swc/core-linux-arm-gnueabihf@npm:1.15.5" + conditions: os=linux & cpu=arm + languageName: node + linkType: hard + +"@swc/core-linux-arm64-gnu@npm:1.15.5": + version: 1.15.5 + resolution: "@swc/core-linux-arm64-gnu@npm:1.15.5" + conditions: os=linux & cpu=arm64 & libc=glibc + languageName: node + linkType: hard + +"@swc/core-linux-arm64-musl@npm:1.15.5": + version: 1.15.5 + resolution: "@swc/core-linux-arm64-musl@npm:1.15.5" + conditions: os=linux & cpu=arm64 & libc=musl + languageName: node + linkType: hard + +"@swc/core-linux-x64-gnu@npm:1.15.5": + version: 1.15.5 + resolution: "@swc/core-linux-x64-gnu@npm:1.15.5" + conditions: os=linux & cpu=x64 & libc=glibc + languageName: node + linkType: hard + +"@swc/core-linux-x64-musl@npm:1.15.5": + version: 1.15.5 + resolution: "@swc/core-linux-x64-musl@npm:1.15.5" + conditions: os=linux & cpu=x64 & libc=musl + languageName: node + linkType: hard + +"@swc/core-win32-arm64-msvc@npm:1.15.5": + version: 1.15.5 + resolution: "@swc/core-win32-arm64-msvc@npm:1.15.5" + conditions: os=win32 & cpu=arm64 + languageName: node + linkType: hard + +"@swc/core-win32-ia32-msvc@npm:1.15.5": + version: 1.15.5 + resolution: "@swc/core-win32-ia32-msvc@npm:1.15.5" + conditions: os=win32 & cpu=ia32 + languageName: node + linkType: hard + +"@swc/core-win32-x64-msvc@npm:1.15.5": + version: 1.15.5 + resolution: "@swc/core-win32-x64-msvc@npm:1.15.5" + conditions: os=win32 & cpu=x64 + languageName: node + linkType: hard + +"@swc/core@npm:^1.15.5": + version: 1.15.5 + resolution: "@swc/core@npm:1.15.5" + dependencies: + "@swc/core-darwin-arm64": "npm:1.15.5" + "@swc/core-darwin-x64": "npm:1.15.5" + "@swc/core-linux-arm-gnueabihf": "npm:1.15.5" + "@swc/core-linux-arm64-gnu": "npm:1.15.5" + "@swc/core-linux-arm64-musl": "npm:1.15.5" + "@swc/core-linux-x64-gnu": "npm:1.15.5" + "@swc/core-linux-x64-musl": "npm:1.15.5" + "@swc/core-win32-arm64-msvc": "npm:1.15.5" + "@swc/core-win32-ia32-msvc": "npm:1.15.5" + "@swc/core-win32-x64-msvc": "npm:1.15.5" + "@swc/counter": "npm:^0.1.3" + "@swc/types": "npm:^0.1.25" + peerDependencies: + "@swc/helpers": ">=0.5.17" + dependenciesMeta: + "@swc/core-darwin-arm64": + optional: true + "@swc/core-darwin-x64": + optional: true + "@swc/core-linux-arm-gnueabihf": + optional: true + "@swc/core-linux-arm64-gnu": + optional: true + "@swc/core-linux-arm64-musl": + optional: true + "@swc/core-linux-x64-gnu": + optional: true + "@swc/core-linux-x64-musl": + optional: true + "@swc/core-win32-arm64-msvc": + optional: true + "@swc/core-win32-ia32-msvc": + optional: true + "@swc/core-win32-x64-msvc": + optional: true + peerDependenciesMeta: + "@swc/helpers": + optional: true + checksum: 10c0/5517d998ad28b6812df46b6c6d9732b3abecb62e94a55d1151e8ede9792b5894b98260681e1de7c33da1a00726c495cff3080ebf97679765c88497e34a978e5a + languageName: node + linkType: hard + +"@swc/counter@npm:^0.1.3": + version: 0.1.3 + resolution: "@swc/counter@npm:0.1.3" + checksum: 10c0/8424f60f6bf8694cfd2a9bca45845bce29f26105cda8cf19cdb9fd3e78dc6338699e4db77a89ae449260bafa1cc6bec307e81e7fb96dbf7dcfce0eea55151356 + languageName: node + linkType: hard + +"@swc/types@npm:^0.1.25": + version: 0.1.25 + resolution: "@swc/types@npm:0.1.25" + dependencies: + "@swc/counter": "npm:^0.1.3" + checksum: 10c0/847a5b20b131281f89d640a7ed4887fb65724807d53d334b230e84b98c21097aa10cd28a074f9ed287a6ce109e443dd4bafbe7dcfb62333d7806c4ea3e7f8aca + languageName: node + linkType: hard + "@tokenizer/inflate@npm:^0.3.1": version: 0.3.1 resolution: "@tokenizer/inflate@npm:0.3.1" @@ -3919,6 +4054,13 @@ __metadata: languageName: node linkType: hard +"@types/validator@npm:^13.15.3": + version: 13.15.10 + resolution: "@types/validator@npm:13.15.10" + checksum: 10c0/3e2e65fcd37dd6961ca3fd0535293d0c42f5911dc3ca44b96f458835e6db2392b678ccbb0c9815d8c0a14e653439e6c62c7b8758a6cd1d6e390551c9e56618ac + languageName: node + linkType: hard + "@types/yargs-parser@npm:*": version: 21.0.3 resolution: "@types/yargs-parser@npm:21.0.3" @@ -4757,7 +4899,7 @@ __metadata: version: 0.0.0-use.local resolution: "back@workspace:." dependencies: - "@baekyangdan/core-utils": "npm:^1.0.9" + "@baekyangdan/core-utils": "npm:^1.0.21" "@eslint/eslintrc": "npm:^3.2.0" "@eslint/js": "npm:^9.18.0" "@fastify/cookie": "npm:^11.0.2" @@ -5140,6 +5282,24 @@ __metadata: languageName: node linkType: hard +"class-transformer@npm:^0.5.1": + version: 0.5.1 + resolution: "class-transformer@npm:0.5.1" + checksum: 10c0/19809914e51c6db42c036166839906420bb60367df14e15f49c45c8c1231bf25ae661ebe94736ee29cc688b77101ef851a8acca299375cc52fc141b64acde18a + languageName: node + linkType: hard + +"class-validator@npm:^0.14.3": + version: 0.14.3 + resolution: "class-validator@npm:0.14.3" + dependencies: + "@types/validator": "npm:^13.15.3" + libphonenumber-js: "npm:^1.11.1" + validator: "npm:^13.15.20" + checksum: 10c0/6d451c359aecb04479b95034b10cca02015d3b6f34480574c618c070e12f3676cb4cdfa76bfa61353356a483ff01326e9ce3f07ef584be6c31806190117f7fa4 + languageName: node + linkType: hard + "cli-cursor@npm:^3.1.0": version: 3.1.0 resolution: "cli-cursor@npm:3.1.0" @@ -8204,6 +8364,13 @@ __metadata: languageName: node linkType: hard +"libphonenumber-js@npm:^1.11.1": + version: 1.12.31 + resolution: "libphonenumber-js@npm:1.12.31" + checksum: 10c0/6617f7c333ac027cc5969330fd094dbc27028f588f016cbc9c4d363b1d9d8162e5e556a48d66d7bdb7e836b7e29d80e8e2d5bd13fa15df355842c9e149c5515a + languageName: node + linkType: hard + "libphonenumber-js@npm:^1.9.43": version: 1.12.28 resolution: "libphonenumber-js@npm:1.12.28" @@ -10987,7 +11154,7 @@ __metadata: languageName: node linkType: hard -"validator@npm:^13.7.0": +"validator@npm:^13.15.20, validator@npm:^13.7.0": version: 13.15.23 resolution: "validator@npm:13.15.23" checksum: 10c0/22a05ec6a98d48d2b6fb34d43ce854af61d15842362d142e64cfca0325d4d0c2d1051d9f9d3a0f741e58ea888f73a35baf7a2a810f5aed0f89183bd5040f0177