Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import com.genius.herewe.core.global.response.CommonResponse;
import com.genius.herewe.core.global.response.ExceptionResponse;
import com.genius.herewe.core.global.response.SingleResponse;
import com.genius.herewe.core.security.dto.AuthRequest;
Expand Down Expand Up @@ -111,4 +113,35 @@ SingleResponse<AuthResponse> authorize(HttpServletResponse response,
)
})
SingleResponse<AuthResponse> reissueToken(HttpServletRequest request, HttpServletResponse response);

@Operation(summary = "로그아웃 API", description = "로그아웃 처리를 하는 API로서, redis & cookie에서 토큰 정보 삭제")
@ApiResponses({
@ApiResponse(
responseCode = "200",
description = "로그아웃 처리 성공"
),
@ApiResponse(
responseCode = "404",
description = "해당 닉네임을 사용하고 있는 사용자를 찾지 못한 경우",
content = @Content(
schema = @Schema(implementation = ExceptionResponse.class),
examples = {
@ExampleObject(
name = "해당 닉네임을 사용하고 있는 사용자를 찾지 못한 경우",
value = """
{
"resultCode": "404",
"code": "MEMBER_NOT_FOUND",
"message": "사용자를 찾을 수 없습니다."
}
"""
)
}
)
)
})
CommonResponse logout(HttpServletResponse response, @RequestParam String nickname);

@Operation(summary = "health check 전용 API", description = "서버가 제대로 동작하는지 확인하는 API")
String healthCheck();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.genius.herewe.core.global.response.CommonResponse;
import com.genius.herewe.core.global.response.SingleResponse;
import com.genius.herewe.core.security.dto.AuthRequest;
import com.genius.herewe.core.security.dto.AuthResponse;
Expand Down Expand Up @@ -39,6 +41,12 @@ public SingleResponse<AuthResponse> reissueToken(HttpServletRequest request, Htt
return new SingleResponse<>(HttpStatus.OK, authResponse);
}

@PostMapping("/auth/logout")
public CommonResponse logout(HttpServletResponse response, @RequestParam String nickname) {
authFacade.logout(response, nickname);
return CommonResponse.ok();
}

@GetMapping("/auth/health-check")
public String healthCheck() {
return "health check OK";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public interface AuthFacade {
Long authorize(HttpServletResponse response, AuthRequest authRequest);

Long reissueToken(HttpServletRequest request, HttpServletResponse response);

void logout(HttpServletResponse response, String nickname);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.genius.herewe.core.security.facade;

import static com.genius.herewe.core.global.exception.ErrorCode.*;
import static com.genius.herewe.core.security.constants.JwtRule.*;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -9,9 +10,11 @@
import com.genius.herewe.core.security.dto.AuthRequest;
import com.genius.herewe.core.security.service.JwtManager;
import com.genius.herewe.core.security.service.token.AuthTokenService;
import com.genius.herewe.core.security.service.token.RefreshTokenService;
import com.genius.herewe.core.user.domain.User;
import com.genius.herewe.core.user.service.UserService;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
Expand All @@ -23,6 +26,7 @@ public class DefaultAuthFacade implements AuthFacade {
private final JwtManager jwtManager;
private final UserService userService;
private final AuthTokenService authTokenService;
private final RefreshTokenService refreshTokenService;

@Transactional
public Long authorize(HttpServletResponse response, AuthRequest authRequest) {
Expand Down Expand Up @@ -55,4 +59,17 @@ public Long reissueToken(HttpServletRequest request, HttpServletResponse respons

return user.getId();
}

@Override
public void logout(HttpServletResponse response, String nickname) {
User user = userService.findByNickname(nickname)
.orElseThrow(() -> new BusinessException(MEMBER_NOT_FOUND));

Cookie cookie = new Cookie(REFRESH_PREFIX.getValue(), null);
cookie.setMaxAge(0);
cookie.setPath("/");
response.addCookie(cookie);

refreshTokenService.delete(user.getId());
}
}