From ce283e24479b0e876783c4c2f776865704711f8d Mon Sep 17 00:00:00 2001 From: Seongjoon-K Date: Tue, 12 Mar 2024 23:02:24 +0900 Subject: [PATCH] refactor: modify kakao login api --- .../floud/demo/controller/AuthController.java | 12 +++++----- .../java/floud/demo/service/AuthService.java | 23 +++++++++++++------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/main/java/floud/demo/controller/AuthController.java b/src/main/java/floud/demo/controller/AuthController.java index f62f9a1..fb2836b 100644 --- a/src/main/java/floud/demo/controller/AuthController.java +++ b/src/main/java/floud/demo/controller/AuthController.java @@ -30,17 +30,17 @@ public RedirectView successGoogleLogin(@RequestParam("code") String code) { public RedirectView redirectToGoogle() { return authService.redirectToGoogle(); } - - @GetMapping("/callback/kakao") - public ApiResponse successKakaooLogin(@RequestParam("code") String code) { - return authService.getKakaoAccessToken(code); - } - @GetMapping("/kakao/login") public RedirectView redirectToKakao() { return authService.redirectToKakao(); } + + @GetMapping("/callback/kakao") + public RedirectView successKakaooLogin(@RequestParam("code") String code) { + return authService.getKakaoAccessToken(code); + } + @GetMapping("/redirect") public RedirectView redirectWithTokens(@RequestParam("access_token") String accessToken, @RequestParam("refresh_token") String refreshToken) { diff --git a/src/main/java/floud/demo/service/AuthService.java b/src/main/java/floud/demo/service/AuthService.java index a43cf09..7e14cb0 100644 --- a/src/main/java/floud/demo/service/AuthService.java +++ b/src/main/java/floud/demo/service/AuthService.java @@ -153,7 +153,7 @@ public RedirectView redirectToKakao() { * 카카오 로그인시 id_token 발급하는 메소드 */ - public ApiResponse getKakaoAccessToken(String code) { + public RedirectView getKakaoAccessToken(String code) { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); @@ -182,16 +182,25 @@ public ApiResponse getKakaoAccessToken(String code) { UsersResponseDto userInfo = getUserInfo(id_token); saveRefreshToken(userInfo.getUsers_id(),refreshToken); - return ApiResponse.success(Success.GET_KAKAO_ACCESS_TOKEN_SUCCESS, TokenResponseDto.builder() - .id_token(id_token) - .refresh_token(refreshToken) - .build()); + String redirectUrl = "http://localhost:3000/redirect"; + redirectUrl += "?access_token=" + id_token + "&refresh_token=" + refreshToken; + + // RedirectView를 사용하여 리다이렉션 수행 + RedirectView redirectView = new RedirectView(); + redirectView.setUrl(redirectUrl); + + return redirectView; + +// return ApiResponse.success(Success.GET_KAKAO_ACCESS_TOKEN_SUCCESS, TokenResponseDto.builder() +// .id_token(id_token) +// .refresh_token(refreshToken) +// .build()); } else { - return ApiResponse.failure(Error.ACCESS_TOKEN_NOT_FOUND); + return null; } } catch (Exception e) { e.printStackTrace(); - return ApiResponse.failure(e); + throw new IllegalArgumentException(); } }