Skip to content

Commit

Permalink
Merge pull request #29 from 9oormthon-univ/dev
Browse files Browse the repository at this point in the history
[#2]🐛Fix: 토큰을 쿠키에
  • Loading branch information
sumin220 authored Nov 22, 2024
2 parents 0698045 + 08bc669 commit b548f34
Showing 1 changed file with 26 additions and 48 deletions.
74 changes: 26 additions & 48 deletions src/main/java/univ/yesummit/global/oauth/OAuth2SuccessHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,56 +44,34 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
throw new RuntimeException(e);
}

// 첫 로그인 여부 확인
boolean firstLogin = memberService.isFirstLogin(memberId);
// 토큰을 HttpOnly 쿠키에 저장
int accessTokenMaxAge = jwtUtils.getAccessExpiration().intValue() / 1000; // 초 단위로 변환
int refreshTokenMaxAge = jwtUtils.getRefreshExpiration().intValue() / 1000;

// 응답 데이터 생성
Map<String, Object> tokens = new HashMap<>();
tokens.put("accessToken", accessToken);
tokens.put("refreshToken", refreshToken);
tokens.put("firstLogin", firstLogin);
ResponseCookie accessTokenCookie = ResponseCookie.from("accessToken", accessToken)
.httpOnly(true)
.secure(true) // HTTPS에서만 동작
.sameSite("None")
.path("/")
.maxAge(accessTokenMaxAge)
.build();

response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
ResponseCookie refreshTokenCookie = ResponseCookie.from("refreshToken", refreshToken)
.httpOnly(true)
.secure(true)
.sameSite("None")
.path("/")
.maxAge(refreshTokenMaxAge)
.build();

// JSON 응답 생성
new ObjectMapper().writeValue(response.getWriter(), tokens);
}
}

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

// // Access Token 쿠키
// ResponseCookie accessTokenCookie = ResponseCookie.from("accessToken", accessToken)
// .httpOnly(true)
// .secure(true) // HTTPS에서만 동작
// .sameSite("None") // Cross-Domain 허용
// .path("/")
// .domain("yesummit.kro.kr") // 도메인 지정
// .maxAge(accessTokenMaxAge)
// .build();
//
// // Refresh Token 쿠키
// ResponseCookie refreshTokenCookie = ResponseCookie.from("refreshToken", refreshToken)
// .httpOnly(true)
// .secure(true)
// .sameSite("None")
// .path("/")
// .domain("yesummit.kro.kr")
// .maxAge(refreshTokenMaxAge)
// .build();
//
//
// response.addHeader("Set-Cookie", accessTokenCookie.toString());
// response.addHeader("Set-Cookie", refreshTokenCookie.toString());
response.addHeader("Set-Cookie", accessTokenCookie.toString());
response.addHeader("Set-Cookie", refreshTokenCookie.toString());

// 첫 로그인 여부에 따라 리다이렉트
// if (memberService.isFirstLogin(memberId)) {
// response.sendRedirect("http://localhost:3000/signup");
// } else {
// response.sendRedirect("http://localhost:3000/home");
// }
// }
//}
if (memberService.isFirstLogin(memberId)) {
response.sendRedirect("http://localhost:3000/signup");
} else {
response.sendRedirect("http://localhost:3000/home");
}
}
}

0 comments on commit b548f34

Please sign in to comment.