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 @@ -42,6 +42,7 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal();
String email = oAuth2User.getName();
Boolean needsEmailUpdate = oAuth2User.getAttribute("needsEmailUpdate");
Boolean needsTermsAgreement = oAuth2User.getAttribute("needsTermsAgreement");

Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
org.springframework.security.authentication.UsernamePasswordAuthenticationToken authToken =
Expand All @@ -58,6 +59,10 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
uriBuilder.queryParam("needsEmailUpdate", true);
}

if (Boolean.TRUE.equals(needsTermsAgreement)) {
uriBuilder.queryParam("needsTermsAgreement", true);
}

String targetUrl = uriBuilder.build()
.encode(StandardCharsets.UTF_8)
.toUriString();
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/com/fitlink/service/OAuth2UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic

Users user;
AuthAccount authAccount;
boolean isNewUser = false; // 완전히 새로운 사용자인지 여부

if (authAccountOpt.isPresent()) {
// 기존 소셜 로그인 사용자
Expand Down Expand Up @@ -112,6 +113,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
}
} else {
// 완전히 새로운 사용자 생성
isNewUser = true;
try {
user = Users.builder()
.email(email)
Expand All @@ -124,15 +126,8 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
user = userRepository.save(user);
entityManager.flush();

// Agreement 기본값(true)으로 생성
Agreement agreement = Agreement.builder()
.user(user)
.privacy(true)
.service(true)
.over14(true)
.location(true)
.build();
agreementRepository.save(agreement);
// 신규 사용자는 약관 동의 페이지를 거쳐야 하므로 Agreement는 생성하지 않음
// 약관 동의 후에 Agreement가 생성됨
} catch (Exception e) {
log.error("Users 저장 실패: email={}, provider={}", email, provider, e);
OAuth2Error oauth2Error = new OAuth2Error(
Expand Down Expand Up @@ -165,12 +160,19 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
}
}

// 신규 사용자인지 확인 (약관 동의 필요 여부)
// 완전히 새로운 사용자이고 Agreement가 없으면 약관 동의 필요
boolean needsTermsAgreement = isNewUser && agreementRepository.findByUser(user).isEmpty();

// OAuth2User 반환 (JWT 토큰 생성에 사용됨)
Map<String, Object> attributesWithFlag = new java.util.HashMap<>(oAuth2User.getAttributes());
if (needsEmailUpdate) {
attributesWithFlag.put("needsEmailUpdate", true);
attributesWithFlag.put("temporaryEmail", email);
}
if (needsTermsAgreement) {
attributesWithFlag.put("needsTermsAgreement", true);
}

return new CustomOAuth2User(
Collections.singletonList(new SimpleGrantedAuthority(user.getRole().name())),
Expand Down