Skip to content

Commit

Permalink
Merge pull request #16 from mju-likelion/bugfix/auth-validate
Browse files Browse the repository at this point in the history
가용 시간 수정 API 로그인 에러 픽스
  • Loading branch information
ndaemy authored Aug 29, 2023
2 parents 53ea52d + 1802812 commit 6dc7ca9
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions src/api/auth/auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {}
2 changes: 1 addition & 1 deletion src/api/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
12 changes: 6 additions & 6 deletions src/api/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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,
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 3 additions & 2 deletions src/api/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -22,7 +23,7 @@ export class UsersController {
return this.usersService.createAppointment(createAppointmentDto);
}

@UseGuards(AuthGuard('jwt'))
@UseGuards(JwtAuthGuard)
@Patch(':roomCode')
update(
@Request() req,
Expand Down
4 changes: 2 additions & 2 deletions src/api/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
Expand Down
28 changes: 26 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down Expand Up @@ -1011,7 +1011,7 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==

"@types/[email protected]":
"@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==
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 6dc7ca9

Please sign in to comment.