From b1615665448f915b37b60a9ac2b036210c91417a Mon Sep 17 00:00:00 2001 From: kimdohyung Date: Thu, 16 Nov 2023 21:01:35 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20swagger=20=EB=AC=B8=EC=84=9C=20=EB=82=B4=EC=9A=A9?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80=20(#13)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/controller/AuthController.java | 41 ++++++++++++++++--- .../backend/domain/auth/dto/LoginUser.java | 12 +++++- .../domain/auth/service/AuthService.java | 9 ++-- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/backend/domain/auth/controller/AuthController.java b/src/main/java/com/backend/domain/auth/controller/AuthController.java index 71510f1..36a520f 100644 --- a/src/main/java/com/backend/domain/auth/controller/AuthController.java +++ b/src/main/java/com/backend/domain/auth/controller/AuthController.java @@ -1,12 +1,19 @@ package com.backend.domain.auth.controller; import com.backend.common.dto.ResponseDto; +import com.backend.domain.auth.dto.Login; +import com.backend.domain.auth.dto.LoginUser; import com.backend.domain.auth.dto.request.JoinRequestDto; import com.backend.domain.auth.dto.request.LoginRequestDto; +import com.backend.error.dto.ErrorResponse; import com.backend.jwt.token.AccessToken; import com.backend.jwt.token.RefreshToken; import com.backend.jwt.token.Token; import com.backend.domain.auth.service.AuthService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletResponse; import jakarta.validation.Valid; @@ -14,7 +21,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.ResponseEntity; -import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; @Slf4j @@ -27,6 +33,11 @@ public class AuthController { private final AuthService authService; + @Operation(summary = "로그인", description = "로그인을 합니다.", + responses = { + @ApiResponse(responseCode = "200", description = "로그인 성공", + content = @Content(schema = @Schema(implementation = ErrorResponse.class))) + }) @PostMapping("/login") public ResponseEntity signIn(@RequestBody @Valid LoginRequestDto loginDto, HttpServletResponse response) { Token token = authService.login(loginDto); @@ -37,13 +48,26 @@ public ResponseEntity signIn(@RequestBody @Valid LoginRequestDto loginDt return ResponseDto.ok("로그인 성공"); } + @Operation(summary = "회원가입", description = "회원가입을 합니다.", + responses = { + @ApiResponse(responseCode = "204", description = "회원가입 성공"), + @ApiResponse(responseCode = "400", description = "이미 존재하는 이메일입니다."), + @ApiResponse(responseCode = "400", description = "잘못된 그룹 종류입니다. " + + "type에 총학생회, 단과대학생회, 과학생회만 입력할 수 있습니다.", + content = @Content(schema = @Schema(implementation = ErrorResponse.class))) + }) @PostMapping("/join") public ResponseEntity signUp(@RequestBody @Valid JoinRequestDto joinDto) { authService.join(joinDto); - return ResponseDto.ok("회원가입 성공"); + return ResponseDto.created("회원가입 성공"); } + @Operation(summary = "토큰 재발급", description = "401에러가 발생한 경우 (AccessToken이 만료된 경우) 토큰을 재발급합니다.", + responses = { + @ApiResponse(responseCode = "204", description = "토큰 재발급 성공", + content = @Content(schema = @Schema(implementation = ErrorResponse.class))) + }) @PostMapping("/reissue") public ResponseEntity reissueToken(@CookieValue(name = "Authorization-refresh") String refreshToken, HttpServletResponse response) { @@ -58,13 +82,18 @@ public ResponseEntity reissueToken(@CookieValue(name = "Authorization-re setAccessToken(response, token.getAccessToken()); setRefreshToken(response, token.getRefreshToken()); - return ResponseDto.ok("토큰 재발급 성공"); + return ResponseDto.created("토큰 재발급 성공"); } + @Operation(summary = "로그아웃", description = "로그아웃을 합니다.", + responses = { + @ApiResponse(responseCode = "204", description = "로그아웃 성공, AccessToken이 필요합니다.", + content = @Content(schema = @Schema(implementation = ErrorResponse.class))) + }) @PostMapping("/logout") - public ResponseEntity logout(Authentication authentication, HttpServletResponse response) { - authService.logout(authentication.getName()); - log.info("이메일: {}", authentication.getName()); + public ResponseEntity logout(@Login LoginUser loginUser, HttpServletResponse response) { + authService.logout(loginUser); + log.info("이메일: {}", loginUser.getEmail()); removeCookie(response); return ResponseDto.ok("로그아웃 성공"); diff --git a/src/main/java/com/backend/domain/auth/dto/LoginUser.java b/src/main/java/com/backend/domain/auth/dto/LoginUser.java index 926c76d..8908a63 100644 --- a/src/main/java/com/backend/domain/auth/dto/LoginUser.java +++ b/src/main/java/com/backend/domain/auth/dto/LoginUser.java @@ -1,4 +1,14 @@ package com.backend.domain.auth.dto; -public record LoginUser(String email, String password) { +import lombok.Builder; +import lombok.Getter; + +@Getter +public class LoginUser { + private String email; + + @Builder + public LoginUser(String email) { + this.email = email; + } } \ No newline at end of file diff --git a/src/main/java/com/backend/domain/auth/service/AuthService.java b/src/main/java/com/backend/domain/auth/service/AuthService.java index 5d72b1c..cc9b647 100644 --- a/src/main/java/com/backend/domain/auth/service/AuthService.java +++ b/src/main/java/com/backend/domain/auth/service/AuthService.java @@ -1,5 +1,6 @@ package com.backend.domain.auth.service; +import com.backend.domain.auth.dto.LoginUser; import com.backend.domain.auth.dto.request.JoinRequestDto; import com.backend.domain.auth.dto.request.LoginRequestDto; import com.backend.jwt.service.JwtProvider; @@ -58,7 +59,7 @@ public Token reissue(RefreshToken refreshToken) { String refreshTokenValue = refreshToken.getData(); log.info("리프레쉬 토큰: {}", refreshTokenValue); - if (jwtProvider.isExpired(refreshTokenValue)) { + if (!jwtProvider.isTokenValid(refreshTokenValue)) { throw new BusinessException(ErrorCode.INVALID_TOKEN); } @@ -72,9 +73,9 @@ public Token reissue(RefreshToken refreshToken) { } @Transactional - public void logout(String email) { - log.info("이메일 : {}", email); - User user = userRepository.findByEmail(email) + public void logout(LoginUser loginUser) { + log.info("이메일 : {}", loginUser.getEmail()); + User user = userRepository.findByEmail(loginUser.getEmail()) .orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND)); user.invalidateRefreshToken();