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,6 +2,7 @@

import com.project.growfit.domain.User.entity.Child;
import com.project.growfit.domain.User.entity.Parent;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -18,4 +19,6 @@ public interface ChildRepository extends JpaRepository<Child, Long> {

boolean existsByCodeNumberAndLoginId(String code, String login_id);
boolean existsByCodeNumber(String code);

List<Child> findAllByParentOrderByCreatedAtDesc(Parent parent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ChildQrCodeResponseDto createQR() throws WriterException {
int height = 200;
String uniqueCode = UUID.randomUUID().toString();

Child child = authenticatedProvider.getAuthenticatedChild();
Child child = authenticatedProvider.getAuthenticatedChildForRegistration();
Long id = child.getId();

log.info("[createQR] QR 코드 생성 요청: user_id={}", parent.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.project.growfit.global.auth.dto.CustomUserDetails;
import com.project.growfit.global.exception.BusinessException;
import com.project.growfit.global.exception.ErrorCode;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down Expand Up @@ -41,6 +42,26 @@ public Child getAuthenticatedChild() {
};
}

public Child getAuthenticatedChildForRegistration() {
CustomUserDetails user = getCurrentUser();
if (user == null) {
throw new BusinessException(ErrorCode.UNAUTHORIZED_CHILD); // 인증되지 않은 사용자
}
return switch (user.getRole()) {
case "ROLE_CHILD" -> getChildByLoginId(user.getUserId());
case "ROLE_PARENT" -> {
Parent parent = getParentByEmail(user.getEmail());
List<Child> children = childRepository.findAllByParentOrderByCreatedAtDesc(parent);

if (children.isEmpty()) {
throw new BusinessException(ErrorCode.CHILD_NOT_FOUND);
}
yield children.get(0); // 가장 최근에 생성된 자녀
}
default -> throw new BusinessException(ErrorCode.FORBIDDEN_ACCESS);
};
}

private CustomUserDetails getCurrentUser() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth == null || !auth.isAuthenticated()) {
Expand Down