-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
src/main/java/com/_119/wepro/auth/service/KakaoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com._119.wepro.auth.service; | ||
|
||
import com._119.wepro.auth.dto.request.AuthRequest.SignInRequest; | ||
import com._119.wepro.auth.dto.response.AuthResponse.SignInResponse; | ||
import com._119.wepro.global.enums.Provider; | ||
import com._119.wepro.auth.client.KakaoOauthClient; | ||
import com._119.wepro.auth.dto.response.KakaoTokenResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class KakaoService { | ||
|
||
private final KakaoOauthClient kakaoOauthClient; | ||
|
||
// 임시 | ||
private static final String LOGIN_URI = "http://localhost:8080/login"; | ||
|
||
@Value("${kakao.client-id}") | ||
private String CLIENT_ID; | ||
|
||
@Value("${kakao.redirect-uri}") | ||
private String REDIRECT_URI; | ||
|
||
@Value("${kakao.client-secret}") | ||
private String CLIENT_SECRET; | ||
|
||
@Value("${kakao.authorization-uri}") | ||
private String KAKAO_AUTH_URL; | ||
|
||
// 백엔드용 | ||
public String generateKakaoRedirectUrl() { | ||
return UriComponentsBuilder.fromUriString(KAKAO_AUTH_URL) | ||
.queryParam("client_id", CLIENT_ID) | ||
.queryParam("redirect_uri", REDIRECT_URI) | ||
.queryParam("response_type", "code") | ||
.build() | ||
.toUriString(); | ||
} | ||
|
||
// 백엔드용 | ||
public SignInResponse handleKakaoCallback(String code) { | ||
KakaoTokenResponse tokenResponse = kakaoOauthClient.kakaoAuth(CLIENT_ID, REDIRECT_URI, code, | ||
CLIENT_SECRET); | ||
String idToken = tokenResponse.getIdToken(); | ||
ResponseEntity<SignInResponse> response = callLoginApiWithIdToken(idToken); | ||
|
||
return response.getBody(); | ||
} | ||
|
||
// 백엔드용 | ||
private ResponseEntity<SignInResponse> callLoginApiWithIdToken(String idToken) { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
SignInRequest signInRequest = new SignInRequest(Provider.KAKAO, idToken); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
HttpEntity<SignInRequest> requestEntity = new HttpEntity<>(signInRequest, headers); | ||
|
||
ResponseEntity<SignInResponse> response = restTemplate.postForEntity(LOGIN_URI, requestEntity, | ||
SignInResponse.class); | ||
|
||
return response; | ||
} | ||
} |