From f3b9b0a11ed028c83e1c0bbfaaa82140536fa88f Mon Sep 17 00:00:00 2001 From: KNU-K Date: Mon, 25 Nov 2024 17:02:51 +0900 Subject: [PATCH] refactor(auth) : apply redis service in auth domain --- .../auth/application/service/auth.service.ts | 10 ++++++++++ .../presentation/controller/auth.controller.ts | 14 ++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/modules/auth/application/service/auth.service.ts b/src/modules/auth/application/service/auth.service.ts index d0d4a56..c1679dd 100644 --- a/src/modules/auth/application/service/auth.service.ts +++ b/src/modules/auth/application/service/auth.service.ts @@ -3,6 +3,7 @@ import { JwtService } from '@nestjs/jwt'; import { UserService } from '@user/application/service/user.service'; import { jwtConfig } from '@config/jwt.config'; +import { RedisService } from '@shared/application/redis/redis.service'; import { AuthResponse, AuthCredentials } from '@auth/application/dto'; @@ -13,6 +14,7 @@ export class AuthService { constructor( private readonly userService: UserService, private readonly jwtService: JwtService, + private readonly redisService: RedisService, ) { const config = jwtConfig(); this.refreshJwtSignOptions = config.refreshJwtSignOptions; @@ -26,6 +28,14 @@ export class AuthService { const payload = { id: user.id, sub: user.email, username: user.name }; return AuthResponse.of(await this.issueAccessToken(payload), await this.issueRefreshToken(payload)); } + async refresh(token: string): Promise { + const payload = this.jwtService.verify(token, this.refreshJwtSignOptions); + const storedToken = await this.redisService.get(payload.sub); + if (storedToken !== token) { + throw new UnauthorizedException(); + } + return AuthResponse.of(await this.issueAccessToken(payload), await this.issueRefreshToken(payload)); + } private async issueAccessToken(payload) { return await this.issueToken(payload, this.accessJwtSignOptions); diff --git a/src/modules/auth/presentation/controller/auth.controller.ts b/src/modules/auth/presentation/controller/auth.controller.ts index 4b25f39..8a7f528 100644 --- a/src/modules/auth/presentation/controller/auth.controller.ts +++ b/src/modules/auth/presentation/controller/auth.controller.ts @@ -1,8 +1,7 @@ -import { Controller, Get, Post, Body, UseGuards, Request } from '@nestjs/common'; +import { Controller, Post, Body } from '@nestjs/common'; import { AuthService } from '@auth/application/service/auth.service'; import { AuthCredentials } from '@auth/application/dto/auth-request.dto'; import { ResponseEntity } from '@common/dto/response-entity.dto'; -import { AuthGuard } from '../guard/auth.guard'; @Controller('auth') export class AuthController { @@ -14,10 +13,9 @@ export class AuthController { return ResponseEntity.success(authResponse, '로그인에 성공'); } - // @Get('test') - // @UseGuards(AuthGuard) - // async test(@Request() req) { - // const userId = req.user.id; // 요청 객체에서 userId 접근 - // return `User ID: ${userId}`; - // } + @Post('refresh') + async refresh(@Body() refreshToken: { token: string }) { + const authResponse = await this.authService.refresh(refreshToken.token); + return ResponseEntity.success(authResponse, '토큰 갱신에 성공'); + } }