Skip to content

Commit

Permalink
[#3]♻️Refactor: 경로 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
sumin220 committed Nov 21, 2024
1 parent baea579 commit 823c95b
Showing 1 changed file with 47 additions and 25 deletions.
72 changes: 47 additions & 25 deletions src/main/java/univ/yesummit/global/oauth/OAuth2SuccessHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package univ.yesummit.global.oauth;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -13,6 +14,8 @@
import univ.yesummit.global.auth.util.JwtUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
Expand All @@ -29,9 +32,11 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
OAuth2Member oAuth2Member = (OAuth2Member) authentication.getPrincipal();
Long memberId = oAuth2Member.getMemberId();

boolean firstLogin = memberService.isFirstLogin(memberId);
// JWT 토큰 생성
String accessToken = jwtUtils.createAccessToken(memberId);
String refreshToken = jwtUtils.createRefreshToken(memberId);
String redirectUrl = firstLogin ? "/additional-info" : "/home";

// Refresh 토큰을 멤버 엔티티에 저장
try {
Expand All @@ -40,35 +45,52 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
throw new RuntimeException(e);
}

// 토큰을 쿠키에 저장
int accessTokenMaxAge = jwtUtils.getAccessExpiration().intValue() / 1000; // 밀리초를 초로 변환
int refreshTokenMaxAge = jwtUtils.getRefreshExpiration().intValue() / 1000;

Cookie accessTokenCookie = new Cookie("accessToken", accessToken);
accessTokenCookie.setHttpOnly(true);
accessTokenCookie.setSecure(true);
accessTokenCookie.setPath("/");
accessTokenCookie.setMaxAge(accessTokenMaxAge);

Cookie refreshTokenCookie = new Cookie("refreshToken", refreshToken);
refreshTokenCookie.setHttpOnly(true);
refreshTokenCookie.setSecure(true);
refreshTokenCookie.setPath("/");
refreshTokenCookie.setMaxAge(refreshTokenMaxAge);

response.addCookie(accessTokenCookie);
response.addCookie(refreshTokenCookie);

// 첫 로그인 여부에 따라 리다이렉트
if (memberService.isFirstLogin(memberId)) {
response.sendRedirect("localhost:3000/additional-info");
} else {
response.sendRedirect("localhost:3000/home");
}
// 응답 데이터 생성
Map<String, Object> responseData = new HashMap<>();
responseData.put("accessToken", accessToken);
responseData.put("refreshToken", refreshToken);
responseData.put("redirectUrl", redirectUrl);

// 응답 설정
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");

// JSON으로 응답
ObjectMapper objectMapper = new ObjectMapper();
String jsonResponse = objectMapper.writeValueAsString(responseData);
response.getWriter().write(jsonResponse);
}
}


// 토큰을 쿠키에 저장
// int accessTokenMaxAge = jwtUtils.getAccessExpiration().intValue() / 1000; // 밀리초를 초로 변환
// int refreshTokenMaxAge = jwtUtils.getRefreshExpiration().intValue() / 1000;
//
// Cookie accessTokenCookie = new Cookie("accessToken", accessToken);
// accessTokenCookie.setHttpOnly(true);
// accessTokenCookie.setSecure(true);
// accessTokenCookie.setPath("/");
// accessTokenCookie.setMaxAge(accessTokenMaxAge);
//
// Cookie refreshTokenCookie = new Cookie("refreshToken", refreshToken);
// refreshTokenCookie.setHttpOnly(true);
// refreshTokenCookie.setSecure(true);
// refreshTokenCookie.setPath("/");
// refreshTokenCookie.setMaxAge(refreshTokenMaxAge);
//
// response.addCookie(accessTokenCookie);
// response.addCookie(refreshTokenCookie);
//
// // 첫 로그인 여부에 따라 리다이렉트
// if (memberService.isFirstLogin(memberId)) {
// response.sendRedirect("localhost:3000/additional-info");
// } else {
// response.sendRedirect("localhost:3000/home");
// }



// @Override
// public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
// log.info("OAuth2SuccessHandler.onAuthenticationSuccess Member Name : {}", authentication.getName());
Expand Down

0 comments on commit 823c95b

Please sign in to comment.