diff --git a/be/src/auth/auth.controller.ts b/be/src/auth/auth.controller.ts index 7310f85..268af05 100644 --- a/be/src/auth/auth.controller.ts +++ b/be/src/auth/auth.controller.ts @@ -1,4 +1,5 @@ import { + Body, Controller, Headers, Post, @@ -11,7 +12,9 @@ import { ApiOperation, ApiResponse, ApiBearerAuth, + ApiBody, } from "@nestjs/swagger"; +import { RefreshTokenDto } from "./dto/refreshToken.dto"; @Controller("auth") export class AuthController { @@ -29,4 +32,13 @@ export class AuthController { signin(@Headers("authorization") authorization: string) { return this.authService.NaverAuth(authorization); } + + @Post("refresh-token") + @ApiOperation({ summary: "accessToken 재발급" }) + @ApiResponse({ status: 200, description: "성공적으로 재발급됨." }) + @ApiResponse({ status: 401, description: "잘못된 refresh token." }) + @ApiBody({ type: RefreshTokenDto }) + checkRefreshToken(@Body() refreshTokenDto: RefreshTokenDto) { + return this.authService.checkRefreshToken(refreshTokenDto.refreshToken); + } } diff --git a/be/src/auth/auth.service.ts b/be/src/auth/auth.service.ts index dce87db..50e314f 100644 --- a/be/src/auth/auth.service.ts +++ b/be/src/auth/auth.service.ts @@ -44,7 +44,7 @@ export class AuthService { if (user) { const payload = { nickName: user.nickName }; const accessToken = this.jwtService.sign(payload); - + const refreshToken = this.jwtService.sign(payload, { secret: "nibobnebob", expiresIn: '7d', @@ -58,4 +58,15 @@ export class AuthService { ); } } + + async checkRefreshToken(refreshToken: string){ + try { + const decoded = this.jwtService.verify(refreshToken, { secret: 'nibobnebob' }); + const payload = { id: decoded.id }; + const accessToken = this.jwtService.sign(payload); + return { accessToken }; + } catch (err) { + throw new HttpException('Invalid refresh token', HttpStatus.UNAUTHORIZED); + } + } } diff --git a/be/src/auth/dto/refreshToken.dto.ts b/be/src/auth/dto/refreshToken.dto.ts new file mode 100644 index 0000000..557ee5c --- /dev/null +++ b/be/src/auth/dto/refreshToken.dto.ts @@ -0,0 +1,9 @@ +import { ApiProperty } from "@nestjs/swagger"; + +export class RefreshTokenDto { + @ApiProperty({ + example: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", + description: "클라이언트가 가지고 있는 refreshToken", + }) + refreshToken: string; + } \ No newline at end of file