diff --git a/src/main/java/team7/inplace/token/application/RefreshTokenFacade.java b/src/main/java/team7/inplace/token/application/RefreshTokenFacade.java index 9afd9b39..b5c611c3 100644 --- a/src/main/java/team7/inplace/token/application/RefreshTokenFacade.java +++ b/src/main/java/team7/inplace/token/application/RefreshTokenFacade.java @@ -35,4 +35,10 @@ public ReIssued getReIssuedRefreshTokenCookie(String username, String refreshTok return TokenCommand.ReIssued.of(reIssuedAccessToken, reIssuedRefreshToken); } + + @Transactional + public void deleteRefreshToken(String refreshToken) { + String username = jwtUtil.getUsername(refreshToken); + refreshTokenService.deleteRefreshToken(username); + } } diff --git a/src/main/java/team7/inplace/token/application/RefreshTokenService.java b/src/main/java/team7/inplace/token/application/RefreshTokenService.java index f087a793..a77290c6 100644 --- a/src/main/java/team7/inplace/token/application/RefreshTokenService.java +++ b/src/main/java/team7/inplace/token/application/RefreshTokenService.java @@ -27,4 +27,7 @@ public void saveRefreshToken(String username, String token) { refreshTokenRepository.save(refreshToken); } + public void deleteRefreshToken(String username) { + refreshTokenRepository.deleteById(username); + } } diff --git a/src/main/java/team7/inplace/token/presentation/RefreshTokenController.java b/src/main/java/team7/inplace/token/presentation/RefreshTokenController.java index 764d93ca..add0c99d 100644 --- a/src/main/java/team7/inplace/token/presentation/RefreshTokenController.java +++ b/src/main/java/team7/inplace/token/presentation/RefreshTokenController.java @@ -8,6 +8,7 @@ import org.springframework.http.ResponseCookie; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CookieValue; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import team7.inplace.security.filter.TokenType; @@ -47,4 +48,23 @@ private void addTokenToCookie(HttpServletResponse response, ReIssued reIssuedTok response.addHeader(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString()); } + @DeleteMapping("/refresh-token") + public ResponseEntity deleteRefreshToken(@CookieValue(value = "refresh_token") Cookie cookie, + HttpServletResponse response + ) { + String refreshToken = cookie.getValue(); + refreshTokenFacade.deleteRefreshToken(refreshToken); + + ResponseCookie accessTokenCookie = CookieUtil.createCookie( + TokenType.ACCESS_TOKEN.getValue(), + ""); + ResponseCookie refreshTokenCookie = CookieUtil.createCookie( + TokenType.REFRESH_TOKEN.getValue(), + ""); + response.addHeader(HttpHeaders.SET_COOKIE, accessTokenCookie.toString()); + response.addHeader(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString()); + + return new ResponseEntity<>(HttpStatus.OK); + } + }