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 @@ -4,6 +4,7 @@
import hs.kr.backend.devpals.domain.user.repository.UserRepository;
import hs.kr.backend.devpals.global.exception.CustomException;
import hs.kr.backend.devpals.global.exception.ErrorException;
import hs.kr.backend.devpals.domain.user.principal.CustomUserDetails;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -36,8 +37,8 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) {

// 요청에 provider 저장
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
HttpServletRequest request = attributes != null ? attributes.getRequest() : null;
if (request != null) {
request.setAttribute("provider", provider);
}

Expand All @@ -48,8 +49,15 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) {
throw new IllegalArgumentException("소셜 로그인 응답에서 email을 찾을 수 없습니다.");
}

UserEntity user = userRepository.findByEmail(email)
.orElseGet(() -> new UserEntity(email, "SOCIAL_LOGIN_USER", name, true));
UserEntity user;

if ("github-auth".equals(provider) && request != null && request.getUserPrincipal() instanceof CustomUserDetails principal) {
user = userRepository.findById(principal.getId())
.orElseThrow(() -> new CustomException(ErrorException.USER_NOT_FOUND));
} else {
user = userRepository.findByEmail(email)
.orElseGet(() -> new UserEntity(email, "SOCIAL_LOGIN_USER", name, true));
}

if ("github".equals(provider) || "github-auth".equals(provider)) {
String githubUrl = oAuth2User.getAttribute("html_url");
Expand Down Expand Up @@ -100,7 +108,7 @@ public static String extractName(String provider, OAuth2User oAuth2User) {
return oAuth2User.getAttribute("name");
case "kakao":
Map<String, Object> kakaoAccount = oAuth2User.getAttribute("kakao_account");
if (kakaoAccount == null) return null;
if (kakaoAccount == null) return null;
Map<String, Object> profile = (Map<String, Object>) kakaoAccount.get("profile");
return profile != null ? (String) profile.get("nickname") : null;
case "naver":
Expand Down Expand Up @@ -131,18 +139,12 @@ private static String fetchPrimaryEmailFromGithub(OAuth2UserRequest userRequest)
);

List<Map<String, Object>> emailList = response.getBody();
if (emailList != null) {
for (Map<String, Object> emailInfo : emailList) {
}
} else {
}
if (emailList == null) return null;

String primaryEmail = emailList.stream()
return emailList.stream()
.filter(e -> Boolean.TRUE.equals(e.get("primary")) && Boolean.TRUE.equals(e.get("verified")))
.map(e -> (String) e.get("email"))
.findFirst()
.orElse(null);

return primaryEmail;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
Expand All @@ -30,24 +32,41 @@ public class Oauth2LoginSuccessHandler extends SimpleUrlAuthenticationSuccessHan
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException {

OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal();
String email = (String) oAuth2User.getAttributes().get("email");

String provider = ((OAuth2AuthenticationToken) authentication).getAuthorizedClientRegistrationId();

if (email == null) {
throw new CustomException(ErrorException.USER_NOT_FOUND);
}

if ("github-auth".equals(provider)) {
String githubUrl = (String) oAuth2User.getAttributes().get("html_url");
OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal();
String githubUrl = oAuth2User.getAttribute("html_url");

String email = oAuth2User.getAttribute("email");

if (email == null || email.isBlank()) {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal instanceof UserDetails userDetails) {
email = userDetails.getUsername();
} else {
email = SecurityContextHolder.getContext().getAuthentication().getName();
}
}

if (email == null || email.isBlank()) {
throw new CustomException(ErrorException.USER_NOT_FOUND);
}

oauthUserService.updateGithubUrl(email, githubUrl);

response.sendRedirect("http://localhost:5173/oauth/github-success?githubUrl=" + githubUrl);
return;
}

// 일반 소셜 로그인 처리
OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal();
String email = oAuth2User.getAttribute("email");

if (email == null || email.isBlank()) {
throw new CustomException(ErrorException.USER_NOT_FOUND);
}

UserEntity user = userRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorException.USER_NOT_FOUND));

Expand Down