diff --git a/src/main/java/com/prgrms/catchtable/jwt/service/RefreshTokenService.java b/src/main/java/com/prgrms/catchtable/jwt/service/RefreshTokenService.java index d505ed88..212a8460 100644 --- a/src/main/java/com/prgrms/catchtable/jwt/service/RefreshTokenService.java +++ b/src/main/java/com/prgrms/catchtable/jwt/service/RefreshTokenService.java @@ -40,4 +40,9 @@ public RefreshToken getRefreshTokenByToken(String refreshToken) { return refreshTokenRepository.findRefreshTokenByToken(refreshToken) .orElseThrow(() -> new NotFoundCustomException(NOT_FOUND_REFRESH_TOKEN)); } + + @Transactional + public void deleteRefreshToken(String email){ + refreshTokenRepository.deleteRefreshTokenByEmail(email); + } } diff --git a/src/main/java/com/prgrms/catchtable/owner/controller/OwnerController.java b/src/main/java/com/prgrms/catchtable/owner/controller/OwnerController.java index a8e781df..5cafb006 100644 --- a/src/main/java/com/prgrms/catchtable/owner/controller/OwnerController.java +++ b/src/main/java/com/prgrms/catchtable/owner/controller/OwnerController.java @@ -1,6 +1,8 @@ package com.prgrms.catchtable.owner.controller; +import com.prgrms.catchtable.common.login.LogIn; import com.prgrms.catchtable.jwt.token.Token; +import com.prgrms.catchtable.owner.domain.Owner; import com.prgrms.catchtable.owner.dto.request.JoinOwnerRequest; import com.prgrms.catchtable.owner.dto.request.LoginOwnerRequest; import com.prgrms.catchtable.owner.dto.response.JoinOwnerResponse; @@ -36,4 +38,10 @@ public ResponseEntity login(@Valid @RequestBody LoginOwnerRequest loginOw return ResponseEntity.ok(responseToken); } + @PostMapping("/logout") + public ResponseEntity logout(@LogIn Owner owner){ + ownerService.logout(owner.getEmail()); + return ResponseEntity.ok("logout"); + } + } diff --git a/src/main/java/com/prgrms/catchtable/owner/service/OwnerService.java b/src/main/java/com/prgrms/catchtable/owner/service/OwnerService.java index 3998044c..425dd3b9 100644 --- a/src/main/java/com/prgrms/catchtable/owner/service/OwnerService.java +++ b/src/main/java/com/prgrms/catchtable/owner/service/OwnerService.java @@ -65,6 +65,11 @@ public Token loginOwner(LoginOwnerRequest loginRequest) { return createTotalToken(loginOwner.getEmail()); } + @Transactional + public void logout(String email){ + refreshTokenService.deleteRefreshToken(email); + } + private void validatePassword(LoginOwnerRequest loginRequest, Owner loginOwner) { if (!passwordEncoder.matches(loginRequest.password(), loginOwner.getPassword())) { throw new BadRequestCustomException(INVALID_EMAIL_OR_PASSWORD); diff --git a/src/test/java/com/prgrms/catchtable/owner/service/OwnerServiceTest.java b/src/test/java/com/prgrms/catchtable/owner/service/OwnerServiceTest.java index 0758a08b..35ed75c3 100644 --- a/src/test/java/com/prgrms/catchtable/owner/service/OwnerServiceTest.java +++ b/src/test/java/com/prgrms/catchtable/owner/service/OwnerServiceTest.java @@ -4,6 +4,8 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import com.prgrms.catchtable.common.Role; @@ -91,6 +93,17 @@ void loginSuccess() { assertThat(ownerService.loginOwner(loginOwnerRequest)).isEqualTo(token); } + @Test + @DisplayName("로그아웃이 성공하면, RefreshToken을 삭제한다.") + void logoutSuccess() { + //when + ownerService.logout(email); + + //then + verify(refreshTokenService, times(1)).deleteRefreshToken(any()); + + } + @Test @DisplayName("해당 이메일의 유저가 존재하지 않으면 로그인을 실패한다") void loginFailureId() {