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/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 ee7726c..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'; @@ -14,8 +13,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, @@ -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 6aa0355..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 f9b028a..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'; @@ -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); 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"