From cfbb9f9b17427039bf03ea498ad7519d42245aaf Mon Sep 17 00:00:00 2001 From: Jjoobob123 <273hur4747@gmail.com> Date: Thu, 30 Mar 2023 14:18:00 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20user=20=EB=B9=84=EB=B0=80=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=B4=88=EA=B8=B0=ED=99=94=20API=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit user 비밀번호 초기화 API 기능 추가 및 비밀번호 업데이트 시 bcrypt 오류 해결 --- src/apis/users/interface/users.interface.ts | 5 +++++ src/apis/users/user.resolver.ts | 13 +++++++++-- src/apis/users/user.service.ts | 25 ++++++++++++++++++--- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/apis/users/interface/users.interface.ts b/src/apis/users/interface/users.interface.ts index c41bb81..a417d60 100644 --- a/src/apis/users/interface/users.interface.ts +++ b/src/apis/users/interface/users.interface.ts @@ -56,3 +56,8 @@ export interface IUsersServiceUpdatePwd { userId: string; updateUserInput: UpdateUserInput; } + +export interface IUsersServiceResetPassword { + email: string; + newPassword: string; +} diff --git a/src/apis/users/user.resolver.ts b/src/apis/users/user.resolver.ts index 04b075a..62e929f 100644 --- a/src/apis/users/user.resolver.ts +++ b/src/apis/users/user.resolver.ts @@ -77,8 +77,17 @@ export class UsersResolver { return this.usersService.update({ userId, updateUserInput }); } + // 비밀번호 찾기(초기화하기) + @Mutation(() => User, { description: 'Return: 비밀번호 초기화하기(찾기)' }) + resetPwd( + @Args('email') email: string, // + @Args('newPassword') newPassword: string, + ): Promise { + return this.usersService.resetPassword({ email, newPassword }); + } + // 이메일 인증번호 전송 - @Mutation(() => String, { description: ' 이메일 인증번호 전송 ' }) + @Mutation(() => String, { description: ' Return: 이메일 인증번호 전송 ' }) getTokenEmail( @Args('email') email: string, // ): Promise { @@ -86,7 +95,7 @@ export class UsersResolver { } // 이메일 인증번호 검증 - @Mutation(() => Boolean, { description: '인증번호 검증' }) + @Mutation(() => Boolean, { description: 'Return: 인증번호 검증' }) checkValidToken( @Args('email') email: string, // @Args('token') token: string, diff --git a/src/apis/users/user.service.ts b/src/apis/users/user.service.ts index 628c9ed..5783661 100644 --- a/src/apis/users/user.service.ts +++ b/src/apis/users/user.service.ts @@ -20,6 +20,7 @@ import { IUsersServiceFindOne, IUsersServiceFindOneByEmail, IUsersServiceFindUserDog, + IUsersServiceResetPassword, IUsersServiceSendEmail, IUsersServiceSendTokenEmail, IUsersServiceUpdate, @@ -173,6 +174,7 @@ export class UsersService { }); return true; } + // 회원 수정하기 async update({ userId, // @@ -180,16 +182,33 @@ export class UsersService { }: IUsersServiceUpdate): Promise { const user = await this.findOne({ userId }); - const hasedPassword = await bcrypt.hash(updateUserInput.password, 10); - + if (updateUserInput.password) { + updateUserInput.password = await bcrypt.hash( + updateUserInput.password, + 10, + ); + } const result = await this.userRepository.save({ ...user, ...updateUserInput, - password: hasedPassword, }); return result; } + // 비밀번호 찾기(초기화하기) + async resetPassword({ + email, + newPassword, + }: IUsersServiceResetPassword): Promise { + const theUser = await this.userRepository.findOne({ + where: { email }, + }); + + theUser.password = await bcrypt.hash(newPassword, 10); + + return await this.userRepository.save(theUser); + } + // 유저 삭제하기(삭제는 나중에) async delete({ userId, //