Skip to content

Commit

Permalink
[#2]✨Feat: 소셜로그인 Success Handler 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
sumin220 committed Nov 19, 2024
1 parent 44b5586 commit df8426d
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/main/java/univ/yesummit/global/oauth/OAuth2SuccessHandler.java
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");
}
}

0 comments on commit df8426d

Please sign in to comment.