-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/univ/yesummit/global/oauth/OAuth2SuccessHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |