From df8426d3cb8a0f8ce5d906a52b0dba8f4c4d5147 Mon Sep 17 00:00:00 2001 From: sonsumin Date: Tue, 19 Nov 2024 23:11:04 +0900 Subject: [PATCH] =?UTF-8?q?[#2]=E2=9C=A8Feat:=20=EC=86=8C=EC=85=9C?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20Success=20Handler=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/oauth/OAuth2SuccessHandler.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/main/java/univ/yesummit/global/oauth/OAuth2SuccessHandler.java diff --git a/src/main/java/univ/yesummit/global/oauth/OAuth2SuccessHandler.java b/src/main/java/univ/yesummit/global/oauth/OAuth2SuccessHandler.java new file mode 100644 index 0000000..d411074 --- /dev/null +++ b/src/main/java/univ/yesummit/global/oauth/OAuth2SuccessHandler.java @@ -0,0 +1,47 @@ +package univ.yesummit.global.oauth; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.security.core.Authentication; +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; +import org.springframework.stereotype.Component; +import univ.yesummit.domain.member.service.MemberService; +import univ.yesummit.global.auth.util.JwtUtils; + +import java.io.IOException; + +@Slf4j +@Component +@RequiredArgsConstructor +public class OAuth2SuccessHandler implements AuthenticationSuccessHandler { + + private final JwtUtils jwtUtils; + private final MemberService memberService; + + @Override + public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { + log.info("OAuth2SuccessHandler.onAuthenticationSuccess Member Name : {}", authentication.getName()); + + OAuth2Member oAuth2Member = (OAuth2Member) authentication.getPrincipal(); + Long memberId = oAuth2Member.getMemberId(); + + // JWT 토큰 발급 + String accessToken = jwtUtils.createAccessToken(memberId); + String refreshToken = jwtUtils.createRefreshToken(); + jwtUtils.sendAccessAndRefreshToken(response, accessToken, refreshToken); + + // 기존 회원인지 확인 + // 기존 회원인지 확인 + if (memberService.isFirstLogin(memberId)) { + // 첫 로그인 시 추가 정보 입력 페이지로 리다이렉트 + response.sendRedirect("/additional-info"); + return; + } + + // 기존 회원이라면 정상 응답 (e.g., 메인 페이지로 리다이렉트) + response.sendRedirect("/home"); + } +}