Skip to content

Commit

Permalink
feat: 소셜 로그인 성공 시 redirect 기능 추가 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyeong-hyeok committed Aug 12, 2023
1 parent e7482a5 commit a110cd0
Showing 1 changed file with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Slf4j
@Component
Expand All @@ -32,13 +34,19 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
// 처음 요청한 회원인 경우 회원가입 페이지로 리다이렉트
if(oAuth2User.getRole() == Role.GUEST) {
String accessToken = jwtService.createAccessToken(oAuth2User.getEmail());
response.addHeader(jwtService.getAccessHeader(), "Bearer " + accessToken);
response.sendRedirect("oauth2/sign-up"); // 프론트의 회원가입 추가 정보 입력 폼으로 리다이렉트
// response.addHeader(jwtService.getAccessHeader(), "Bearer " + accessToken);
// response.sendRedirect("oauth2/sign-up"); // 프론트의 회원가입 추가 정보 입력 폼으로 리다이렉트

jwtService.sendAccessAndRefreshToken(response, accessToken, null);
// jwtService.sendAccessAndRefreshToken(response, accessToken, null);
// User findUser = userRepository.findByEmail(oAuth2User.getEmail())
// .orElseThrow(() -> new IllegalArgumentException("이메일에 해당하는 유저가 없습니다."));
// findUser.authorizeUser();
response.sendRedirect(UriComponentsBuilder.fromUriString("https://mapdagu.site/login/callback")
.queryParam("accessToken", accessToken)
.build()
.encode(StandardCharsets.UTF_8)

.toUriString());
} else {
loginSuccess(response, oAuth2User); // 로그인에 성공한 경우 access, refresh 토큰 생성
}
Expand All @@ -52,10 +60,16 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
private void loginSuccess(HttpServletResponse response, CustomOAuth2User oAuth2User) throws IOException {
String accessToken = jwtService.createAccessToken(oAuth2User.getEmail());
String refreshToken = jwtService.createRefreshToken(oAuth2User.getEmail());
response.addHeader(jwtService.getAccessHeader(), "Bearer " + accessToken);
response.addHeader(jwtService.getRefreshHeader(), "Bearer " + refreshToken);
// response.addHeader(jwtService.getAccessHeader(), "Bearer " + accessToken);
// response.addHeader(jwtService.getRefreshHeader(), "Bearer " + refreshToken);

jwtService.sendAccessAndRefreshToken(response, accessToken, refreshToken);
// jwtService.sendAccessAndRefreshToken(response, accessToken, refreshToken);
jwtService.updateRefreshToken(oAuth2User.getEmail(), refreshToken);
response.sendRedirect(UriComponentsBuilder.fromUriString("https://mapdagu.site/login/callback")
.queryParam("accessToken", accessToken)
.queryParam("refreshToken", refreshToken)
.build()
.encode(StandardCharsets.UTF_8)
.toUriString());
}
}

0 comments on commit a110cd0

Please sign in to comment.