Skip to content

Commit

Permalink
feat: 소셜 로그인 성공 시 access, refresh 토큰 반환 설정 (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyeong-hyeok committed Aug 13, 2023
1 parent e32a1ae commit d844375
Showing 1 changed file with 3 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,24 @@
public class OAuth2LoginSuccessHandler implements AuthenticationSuccessHandler {

private final JwtService jwtService;
private final MemberRepository memberRepository;

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
log.info("OAuth2 Login 성공!");
try {
CustomOAuth2User oAuth2User = (CustomOAuth2User) authentication.getPrincipal();

// 처음 요청한 회원인 경우 회원가입 페이지로 리다이렉트
if(oAuth2User.getRole() == Role.GUEST) {
String accessToken = jwtService.createAccessToken(oAuth2User.getEmail());
// response.addHeader(jwtService.getAccessHeader(), "Bearer " + accessToken);
// response.sendRedirect("oauth2/sign-up"); // 프론트의 회원가입 추가 정보 입력 폼으로 리다이렉트

// jwtService.sendAccessAndRefreshToken(response, accessToken, null);
// User findUser = userRepository.findByEmail(oAuth2User.getEmail())
// .orElseThrow(() -> new IllegalArgumentException("이메일에 해당하는 유저가 없습니다."));
// findUser.authorizeUser();
String role = oAuth2User.getAuthorities().toString();
response.sendRedirect(UriComponentsBuilder.fromUriString("https://mapdagu.site/login/callback")
.queryParam("accessToken", accessToken)
.queryParam("role", role.substring(6, role.length()-1))
.build()
.encode(StandardCharsets.UTF_8)
.toUriString());
} else {
loginSuccess(response, oAuth2User); // 로그인에 성공한 경우 access, refresh 토큰 생성
}
loginSuccess(response, oAuth2User); // 로그인에 성공한 경우 access, refresh 토큰 생성
} catch (Exception e) {
throw e;
}

}

// TODO : 소셜 로그인 시에도 무조건 토큰 생성하지 말고 JWT 인증 필터처럼 RefreshToken 유/무에 따라 다르게 처리해보기
private void loginSuccess(HttpServletResponse response, CustomOAuth2User oAuth2User) throws IOException {
log.info("Access token, Refresh token 생성 후 리다이렉트 실행");
String accessToken = jwtService.createAccessToken(oAuth2User.getEmail());
String refreshToken = jwtService.createRefreshToken(oAuth2User.getEmail());
// response.addHeader(jwtService.getAccessHeader(), "Bearer " + accessToken);
// response.addHeader(jwtService.getRefreshHeader(), "Bearer " + refreshToken);

// jwtService.sendAccessAndRefreshToken(response, accessToken, refreshToken);
jwtService.updateRefreshToken(oAuth2User.getEmail(), refreshToken);

String role = oAuth2User.getAuthorities().toString();
response.sendRedirect(UriComponentsBuilder.fromUriString("https://mapdagu.site/login/callback")
.queryParam("accessToken", accessToken)
Expand Down

0 comments on commit d844375

Please sign in to comment.