Skip to content

Commit

Permalink
Merge pull request #181 from SPACE-FOR-SPACE/fix/#179
Browse files Browse the repository at this point in the history
이메일 인증 토큰 중복 에러 해결
  • Loading branch information
YunChan-Oh authored Nov 27, 2024
2 parents 08c5c0f + f67f427 commit 0136aef
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.space.server.common.exception.security.SpaceSecurityException;
import com.space.server.common.logging.LoggingUtils;
import com.space.server.domain.mail.exception.value.EmailVerificationStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand All @@ -13,6 +14,11 @@
@RestControllerAdvice
public class GlobalExceptionHandler {


@Value("${server.url}")
private String baseUrl;


@ExceptionHandler(SpaceException.class)
public ResponseEntity<ErrorResponse> handleApplicationException(SpaceException exception) {
LoggingUtils.warn(exception);
Expand Down Expand Up @@ -60,6 +66,7 @@ public ModelAndView handleSpaceEmailException(SpaceMailException exception) {
}

modelAndView.addObject("status", status);
modelAndView.addObject("baseUrl", baseUrl);

return modelAndView;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public void joinProcess(JoinUserRequest joinUserRequest) {

userRepository.save(user);

Item head = itemReader.findById(1L);
Item theme = itemReader.findById(2L);
Item head = itemReader.findByName("기본 머리");
Item theme = itemReader.findByName("기본 테마");

Inventory In_head = new Inventory(head, user);
Inventory In_theme = new Inventory(theme, user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
@Repository
public interface ItemRepository extends JpaRepository<Item, Long> {
List<Item> findByCategory(Category category);

Item findByName(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public Item findById(Long itemId) {
.orElseThrow(ItemNotFoundException::new);
}

public Item findByName(String itemName) {
return itemRepository.findByName(itemName);
}

public List<Item> findAll() {
return itemRepository.findAll();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public interface EmailTokenRepository extends JpaRepository<EmailToken, Long> {
EmailToken findByToken(String token);
EmailToken findByEmailAndIsVerifiedFalse(String email);
void deleteByEmail(String email);
EmailToken findByEmailAndIsVerified(String email, boolean verified);
void deleteByExpiryDateBefore(LocalDateTime now);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@RestController
@RequiredArgsConstructor
@Tag(name = "Email", description = "이메일 인증 API")
public class EmailTokenController {

@Value("${server.url}")
private String baseUrl;

private final EmailTokenService emailTokenService;

@ResponseStatus(HttpStatus.OK)
Expand All @@ -33,6 +39,7 @@ public ModelAndView verifyEmail(@RequestParam String token) {

ModelAndView modelAndView = new ModelAndView("verification-result");
modelAndView.addObject("status", EmailVerificationStatus.SUCCESS.name());
modelAndView.addObject("baseUrl", baseUrl);

return modelAndView;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
import com.space.server.domain.user.domain.repository.UserRepository;
import com.space.server.domain.user.exception.UserExistedException;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
@Transactional
Expand All @@ -27,11 +24,7 @@ public void sendVerificationEmail(String email) {
throw new UserExistedException();
}

EmailToken existingToken =
emailTokenRepository.findByEmailAndIsVerifiedFalse(email);
if (existingToken != null) {
emailTokenRepository.delete(existingToken);
}
emailTokenRepository.deleteByEmail(email);

EmailToken emailToken = EmailToken.builder()
.email(email)
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ server:
port: 8081
serverAddress: localhost
serverName: local_server
url: http://localhost:3000
url: http://localhost:5173
proxy:
url: http://localhost:8081

Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/templates/verification-result.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ <h2 class="text-2xl font-bold mt-4">이미 인증됨</h2>
</div>

<div class="mt-8 space-y-4">
<a href="http://localhost:5173" class="block w-full px-6 py-3 text-white bg-blue-600 rounded-lg hover:bg-blue-700 text-center transition-colors duration-200">
<a th:href="${baseUrl}" class="block w-full px-6 py-3 text-white bg-blue-600 rounded-lg hover:bg-blue-700 text-center transition-colors duration-200">
홈으로 돌아가기
</a>
<a href="http://localhost:5173/login" class="block w-full px-6 py-3 text-blue-600 bg-white border-2 border-blue-600 rounded-lg hover:bg-blue-50 text-center transition-colors duration-200">
<a th:href="${baseUrl} + '/login'" class="block w-full px-6 py-3 text-blue-600 bg-white border-2 border-blue-600 rounded-lg hover:bg-blue-50 text-center transition-colors duration-200">
로그인하기
</a>
</div>
Expand Down

0 comments on commit 0136aef

Please sign in to comment.