Skip to content

Commit

Permalink
hotfix: 로컬 리다이렉트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
JjungminLee committed Oct 6, 2023
1 parent b9ef7aa commit 6e26aac
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,12 @@ public ResponseEntity<OAuthLoginResponse> loginKakao(@RequestBody KakaoLoginRequ
return ResponseEntity.ok(oAuthLoginService.login(kakaoLoginRequest.getAuthorizationCode()));
}

@Operation(description = "카카오 로컬 로그인")
@PostMapping("/local")
public ResponseEntity<OAuthLoginResponse> localLoginKakao(@RequestBody KakaoLoginRequest kakaoLoginRequest) {
System.err.println(kakaoLoginRequest.getAuthorizationCode());
return ResponseEntity.ok(oAuthLoginService.localLogin(kakaoLoginRequest.getAuthorizationCode()));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
public interface OAuthUseCase {
String requestAccessToken(String authorizationCode);

String requestLocalAccessToken(String authorizationCode);

KakaoInfoResponse requestOauthInfo(String accessToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ public OAuthLoginResponse login(String authorizationCode) {
.build();
}

@Transactional
public OAuthLoginResponse localLogin(String authorizationCode) {

String accessToken = oAuthService.requestLocalAccessToken(authorizationCode);
KakaoInfoResponse kakaoInfoResponse =oAuthService.requestOauthInfo(accessToken);
String email=kakaoInfoResponse.getKakaoAccount().getEmail();
Member member=memberRepository.findByEmail(email).orElseGet(
()->saveMember(kakaoInfoResponse)
);
System.err.println(kakaoInfoResponse.getKakaoAccount().getEmail());
System.err.println(member.getEmail());
String serviceAccessToken= jwtService.createAccessToken(email);
String serviceRefreshToken= jwtService.createRefreshToken(email);

member.updateRefreshToken(serviceRefreshToken);
return OAuthLoginResponse.builder()
.memberEmail(member.getEmail())
.accessToken(serviceAccessToken)
.refreshToken(serviceRefreshToken)
.build();
}

@Transactional
public Member saveMember(KakaoInfoResponse kakaoInfoResponse) {
Gender memberGender;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public class OAuthService implements OAuthUseCase {
@Value("${oauth.kakao.url.redirect-uri}")
private String redirectUri;

@Value("${oauth.kakao.url.redirect-local}")
private String redirectLocalUri;


private final RestTemplate restTemplate;


Expand Down Expand Up @@ -69,6 +73,29 @@ public String requestAccessToken(String authorizationCode) {

}

@Override
public String requestLocalAccessToken(String authorizationCode) {

String tokenUri = authUrl + "/oauth/token";

HttpHeaders httpHeaders=new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
httpHeaders.add("Accept", "application/json");

MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
body.add("code",authorizationCode);
body.add("grant_type", GRANT_TYPE);
body.add("client_id", clientId);
body.add("redirect_uri",redirectLocalUri);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(body,httpHeaders);

restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
KakaoTokens response = restTemplate.postForObject(tokenUri, request, KakaoTokens.class);
assert response != null;
return response.getAccessToken();

}


@Override
public KakaoInfoResponse requestOauthInfo(String accessToken) {
Expand Down

0 comments on commit 6e26aac

Please sign in to comment.