From bf90c6143122ced412f680b12994f8ea5174c4ea Mon Sep 17 00:00:00 2001 From: ndaemy Date: Sat, 19 Aug 2023 03:22:28 +0900 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20login=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20createToken=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=A1=9C?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/auth/auth.service.ts | 4 ++-- src/api/users/users.service.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/api/auth/auth.service.ts b/src/api/auth/auth.service.ts index ee7726c..929ea1e 100644 --- a/src/api/auth/auth.service.ts +++ b/src/api/auth/auth.service.ts @@ -14,8 +14,8 @@ export class AuthService { private readonly prismaService: PrismaService ) {} - async login(userName: string, password: string) { - const payload = { userName, password }; + createToken(userName: string) { + const payload = { userName }; return { accessToken: this.jwtService.sign(payload, { secret: this.config.jwtSecret, diff --git a/src/api/users/users.service.ts b/src/api/users/users.service.ts index f9b028a..c23c508 100644 --- a/src/api/users/users.service.ts +++ b/src/api/users/users.service.ts @@ -64,7 +64,7 @@ export class UsersService { } const hashedPassword = await hash(password, 10); - const token = this.authService.login(username, password); + const token = this.authService.createToken(username); if (notFoundErrors.length > 0) { throw new NotFoundException(notFoundErrors); From af2aea8706ee933f38704d46e80f51da41286245 Mon Sep 17 00:00:00 2001 From: ndaemy Date: Sun, 20 Aug 2023 03:23:59 +0900 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20=EB=AA=A8=EB=93=A0=20userName=20?= =?UTF-8?q?=EC=9D=84=20username=20=EC=9C=BC=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/auth/auth.service.ts | 4 ++-- src/api/auth/jwt.strategy.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/api/auth/auth.service.ts b/src/api/auth/auth.service.ts index 929ea1e..f181619 100644 --- a/src/api/auth/auth.service.ts +++ b/src/api/auth/auth.service.ts @@ -14,8 +14,8 @@ export class AuthService { private readonly prismaService: PrismaService ) {} - createToken(userName: string) { - const payload = { userName }; + createToken(username: string) { + const payload = { username }; return { accessToken: this.jwtService.sign(payload, { secret: this.config.jwtSecret, diff --git a/src/api/auth/jwt.strategy.ts b/src/api/auth/jwt.strategy.ts index 6aa0355..ff15c5e 100644 --- a/src/api/auth/jwt.strategy.ts +++ b/src/api/auth/jwt.strategy.ts @@ -20,8 +20,8 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') { } async validate(payload: any) { - const { userName, password } = payload; - const user = await this.authService.validateUser(userName, password); + const { username, password } = payload; + const user = await this.authService.validateUser(username, password); //에러를 발생시키지 않기로 했으므로 validate를 요청했을때의 리턴값이 존재하지 않을경우 error를 푸시하는 방향으로 하겠습니다. return user; } From 9d2bbf00814ab775119067b94abb35ad7447a5ae Mon Sep 17 00:00:00 2001 From: ndaemy Date: Mon, 21 Aug 2023 03:26:29 +0900 Subject: [PATCH 3/4] =?UTF-8?q?chore:=20@types/passport-jwt=20=EC=84=A4?= =?UTF-8?q?=EC=B9=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + yarn.lock | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 8d079d8..e90a559 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "@types/jest": "29.5.0", "@types/lodash": "^4.14.196", "@types/node": "18.15.11", + "@types/passport-jwt": "^3.0.9", "@types/supertest": "^2.0.11", "@typescript-eslint/eslint-plugin": "^5.0.0", "@typescript-eslint/parser": "^5.0.0", diff --git a/yarn.lock b/yarn.lock index d468b98..9068ef1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -957,7 +957,7 @@ "@types/range-parser" "*" "@types/send" "*" -"@types/express@^4.17.13": +"@types/express@*", "@types/express@^4.17.13": version "4.17.17" resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.17.tgz#01d5437f6ef9cfa8668e616e13c2f2ac9a491ae4" integrity sha512-Q4FmmuLGBG58btUnfS1c1r/NQdlp3DMfGDGig8WhfpA2YRUtEkxAjkZb0yvplJGYdF1fsQ81iMDcH24sSCNC/Q== @@ -1011,7 +1011,7 @@ resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ== -"@types/jsonwebtoken@9.0.2": +"@types/jsonwebtoken@*", "@types/jsonwebtoken@9.0.2": version "9.0.2" resolved "https://registry.yarnpkg.com/@types/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz#9eeb56c76dd555039be2a3972218de5bd3b8d83e" integrity sha512-drE6uz7QBKq1fYqqoFKTDRdFCPHd5TCub75BM+D+cMx7NU9hUz7SESLfC2fSCXVFMO5Yj8sOWHuGqPgjc+fz0Q== @@ -1048,6 +1048,30 @@ resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0" integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA== +"@types/passport-jwt@^3.0.9": + version "3.0.9" + resolved "https://registry.yarnpkg.com/@types/passport-jwt/-/passport-jwt-3.0.9.tgz#6c74c71e133206c697344feaf6e6044e01fe2d1d" + integrity sha512-5XJt+79emfgpuBvBQusUPylFIVtW1QVAAkTRwCbRJAmxUjmLtIqUU6V1ovpnHPu6Qut3mR5Juc+s7kd06roNTg== + dependencies: + "@types/express" "*" + "@types/jsonwebtoken" "*" + "@types/passport-strategy" "*" + +"@types/passport-strategy@*": + version "0.2.35" + resolved "https://registry.yarnpkg.com/@types/passport-strategy/-/passport-strategy-0.2.35.tgz#e52f5212279ea73f02d9b06af67efe9cefce2d0c" + integrity sha512-o5D19Jy2XPFoX2rKApykY15et3Apgax00RRLf0RUotPDUsYrQa7x4howLYr9El2mlUApHmCMv5CZ1IXqKFQ2+g== + dependencies: + "@types/express" "*" + "@types/passport" "*" + +"@types/passport@*": + version "1.0.12" + resolved "https://registry.yarnpkg.com/@types/passport/-/passport-1.0.12.tgz#7dc8ab96a5e895ec13688d9e3a96920a7f42e73e" + integrity sha512-QFdJ2TiAEoXfEQSNDISJR1Tm51I78CymqcBa8imbjo6dNNu+l2huDxxbDEIoFIwOSKMkOfHEikyDuZ38WwWsmw== + dependencies: + "@types/express" "*" + "@types/prettier@^2.1.5": version "2.7.2" resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.7.2.tgz#6c2324641cc4ba050a8c710b2b251b377581fbf0" From 18028125f063dddbe0a5f361fd67ce192f3454fa Mon Sep 17 00:00:00 2001 From: k013600 Date: Wed, 30 Aug 2023 01:26:14 +0900 Subject: [PATCH 4/4] =?UTF-8?q?fix=20:=20=ED=86=A0=ED=81=B0=20=EC=9D=B8?= =?UTF-8?q?=EC=A6=9D=20=EA=B3=BC=EC=A0=95=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/auth/auth.guard.ts | 5 +++++ src/api/auth/auth.module.ts | 2 +- src/api/auth/auth.service.ts | 8 ++++---- src/api/auth/{jwt.strategy.ts => auth.strategy.ts} | 7 +++---- src/api/users/users.controller.ts | 5 +++-- src/api/users/users.service.ts | 2 +- 6 files changed, 17 insertions(+), 12 deletions(-) create mode 100644 src/api/auth/auth.guard.ts rename src/api/auth/{jwt.strategy.ts => auth.strategy.ts} (71%) diff --git a/src/api/auth/auth.guard.ts b/src/api/auth/auth.guard.ts new file mode 100644 index 0000000..2155290 --- /dev/null +++ b/src/api/auth/auth.guard.ts @@ -0,0 +1,5 @@ +import { Injectable } from '@nestjs/common'; +import { AuthGuard } from '@nestjs/passport'; + +@Injectable() +export class JwtAuthGuard extends AuthGuard('jwt') {} diff --git a/src/api/auth/auth.module.ts b/src/api/auth/auth.module.ts index e26694a..35e61ee 100644 --- a/src/api/auth/auth.module.ts +++ b/src/api/auth/auth.module.ts @@ -7,7 +7,7 @@ import authConfig from 'src/config/authConfig'; import { PrismaModule } from '@/prisma/prisma.module'; import { AuthService } from './auth.service'; -import { JwtStrategy } from './jwt.strategy'; +import { JwtStrategy } from './auth.strategy'; @Module({ imports: [ diff --git a/src/api/auth/auth.service.ts b/src/api/auth/auth.service.ts index f181619..321b727 100644 --- a/src/api/auth/auth.service.ts +++ b/src/api/auth/auth.service.ts @@ -2,7 +2,6 @@ import { JwtService } from '@nestjs/jwt'; import { Inject, Injectable } from '@nestjs/common'; import authConfig from 'src/config/authConfig'; import { ConfigType } from '@nestjs/config'; -import { compare } from 'bcrypt'; import { PrismaService } from '@/prisma/prisma.service'; @@ -24,13 +23,14 @@ export class AuthService { }; } - async validateUser(username: string, password: string) { - // TODO: 임시땜빵 - 추후 room code 를 같이 받도록 수정해야 함 + async validateUser(username: string) { const user = await this.prismaService.user.findFirst({ where: { username }, + include: { room: true }, }); - if (!user || (user && !compare(password, user.password))) return null; + if (!user) return null; + return user; } } diff --git a/src/api/auth/jwt.strategy.ts b/src/api/auth/auth.strategy.ts similarity index 71% rename from src/api/auth/jwt.strategy.ts rename to src/api/auth/auth.strategy.ts index ff15c5e..5ffadb4 100644 --- a/src/api/auth/jwt.strategy.ts +++ b/src/api/auth/auth.strategy.ts @@ -18,11 +18,10 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') { secretOrKey: config.jwtSecret, }); } - + //토큰 검증 async validate(payload: any) { - const { username, password } = payload; - const user = await this.authService.validateUser(username, password); - //에러를 발생시키지 않기로 했으므로 validate를 요청했을때의 리턴값이 존재하지 않을경우 error를 푸시하는 방향으로 하겠습니다. + const { username } = payload; + const user = await this.authService.validateUser(username); return user; } } diff --git a/src/api/users/users.controller.ts b/src/api/users/users.controller.ts index 4935bbf..99eaf18 100644 --- a/src/api/users/users.controller.ts +++ b/src/api/users/users.controller.ts @@ -7,7 +7,8 @@ import { Param, Request, } from '@nestjs/common'; -import { AuthGuard } from '@nestjs/passport'; + +import { JwtAuthGuard } from '../auth/auth.guard'; import { UsersService } from './users.service'; import { CreateAppointmentDto } from './dto/create-appointment.dto'; @@ -22,7 +23,7 @@ export class UsersController { return this.usersService.createAppointment(createAppointmentDto); } - @UseGuards(AuthGuard('jwt')) + @UseGuards(JwtAuthGuard) @Patch(':roomCode') update( @Request() req, diff --git a/src/api/users/users.service.ts b/src/api/users/users.service.ts index c23c508..7bd93cd 100644 --- a/src/api/users/users.service.ts +++ b/src/api/users/users.service.ts @@ -4,7 +4,7 @@ import { NotFoundException, } from '@nestjs/common'; import { hash } from 'bcrypt'; -import { Room, User } from '@prisma/client'; +import { User } from '@prisma/client'; import { PrismaService } from '@/prisma/prisma.service';