Skip to content

Commit

Permalink
feat : accessToken 재발급 #35
Browse files Browse the repository at this point in the history
- refreshToken이 유효한 경우 accessToken 재발급
- refreshToken이 유효하지 않은 경우 401 에러 응답
  • Loading branch information
LeeTH916 committed Nov 20, 2023
1 parent f0eb246 commit fcabaec
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
12 changes: 12 additions & 0 deletions be/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Body,
Controller,
Headers,
Post,
Expand All @@ -11,7 +12,9 @@ import {
ApiOperation,
ApiResponse,
ApiBearerAuth,
ApiBody,
} from "@nestjs/swagger";
import { RefreshTokenDto } from "./dto/refreshToken.dto";

@Controller("auth")
export class AuthController {
Expand All @@ -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);
}
}
13 changes: 12 additions & 1 deletion be/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
}
}
}
9 changes: 9 additions & 0 deletions be/src/auth/dto/refreshToken.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApiProperty } from "@nestjs/swagger";

export class RefreshTokenDto {
@ApiProperty({
example: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
description: "클라이언트가 가지고 있는 refreshToken",
})
refreshToken: string;
}

0 comments on commit fcabaec

Please sign in to comment.