Skip to content

Commit

Permalink
feat: 로그인 코드 swagger 문서 내용 추가 (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdohyung committed Nov 16, 2023
1 parent 13a0a3a commit b161566
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
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;
import lombok.RequiredArgsConstructor;
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
Expand All @@ -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<String> signIn(@RequestBody @Valid LoginRequestDto loginDto, HttpServletResponse response) {
Token token = authService.login(loginDto);
Expand All @@ -37,13 +48,26 @@ public ResponseEntity<String> 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<String> 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<String> reissueToken(@CookieValue(name = "Authorization-refresh") String refreshToken,
HttpServletResponse response) {
Expand All @@ -58,13 +82,18 @@ public ResponseEntity<String> 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<String> logout(Authentication authentication, HttpServletResponse response) {
authService.logout(authentication.getName());
log.info("이메일: {}", authentication.getName());
public ResponseEntity<String> logout(@Login LoginUser loginUser, HttpServletResponse response) {
authService.logout(loginUser);
log.info("이메일: {}", loginUser.getEmail());
removeCookie(response);

return ResponseDto.ok("로그아웃 성공");
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/backend/domain/auth/dto/LoginUser.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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();
Expand Down

0 comments on commit b161566

Please sign in to comment.