From ecd7c8ff0ef800c5ead2eca3a6f5ae4ea11c86df Mon Sep 17 00:00:00 2001 From: adorableco Date: Thu, 8 Aug 2024 13:35:18 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=20Fix:=20Google=20Login=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit part of swm-147 related to: #168 --- .../phote/controller/AuthController.kt | 59 ++++++++++++------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/com/swm_standard/phote/controller/AuthController.kt b/src/main/kotlin/com/swm_standard/phote/controller/AuthController.kt index eadd29a..c74edfd 100644 --- a/src/main/kotlin/com/swm_standard/phote/controller/AuthController.kt +++ b/src/main/kotlin/com/swm_standard/phote/controller/AuthController.kt @@ -7,11 +7,15 @@ import com.swm_standard.phote.service.KaKaoAuthService import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.tags.Tag import org.springframework.beans.factory.annotation.Value +import org.springframework.http.HttpHeaders +import org.springframework.http.ResponseCookie +import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController import org.springframework.web.servlet.view.RedirectView +import org.springframework.web.util.UriComponentsBuilder @RestController @RequestMapping("/api/auth") @@ -20,12 +24,6 @@ class AuthController( private val googleAuthService: GoogleAuthService, private val kaKaoAuthService: KaKaoAuthService, ) { - @Value("\${GOOGLE_CLIENT_ID}") - lateinit var clientId: String - - @Value("\${REDIRECT_URI}") - lateinit var redirectUri: String - @Value("\${KAKAO_REST_API_KEY}") lateinit var kakaokey: String @@ -34,18 +32,7 @@ class AuthController( @Operation(summary = "google-login", description = "구글 로그인/회원가입") @GetMapping("/google-login") - fun googleLogin(): RedirectView { - val redirectView = RedirectView() - redirectView.url = - "https://accounts.google.com/o/oauth2/v2/auth?client_id=$clientId&" + - "response_type=code&redirect_uri=$redirectUri&scope=https://www.googleapis.com/auth/userinfo.email" - - return redirectView - } - - @Operation(summary = "google user info", description = "구글 로그인 유저 정보 조회") - @GetMapping("/token") - fun getUserInfo( + fun googleLogin( @RequestParam code: String, ): BaseResponse { val accessToken = googleAuthService.getTokenFromGoogle(code) @@ -70,11 +57,41 @@ class AuthController( @GetMapping("/kakao-token") fun getKakaoUserInfo( @RequestParam code: String, - ): BaseResponse { + ): ResponseEntity { + // 카카오에서 액세스 토큰을 가져옵니다. val accessToken = kaKaoAuthService.getTokenFromKakao(code) + // 액세스 토큰을 사용하여 사용자 정보를 가져옵니다. val userInfo = kaKaoAuthService.getUserInfoFromKakao(accessToken) - val message = if (userInfo.isMember == false) "회원가입 성공" else "로그인 성공" - return BaseResponse(msg = message, data = userInfo) + // 쿠키에 액세스 토큰을 저장합니다. + val tokenCookie = + ResponseCookie + .from("accessToken", accessToken) + .httpOnly(true) + .secure(true) + .path("/") + .maxAge(7 * 24 * 60 * 60) // 7일 동안 유효 + .build() + + // 리다이렉트 URL 생성 + val redirectUrl = + if (userInfo.isMember == false) { + UriComponentsBuilder + .fromUriString("https://pho-te.com/workbook") + .build() + .toUriString() + } else { + UriComponentsBuilder + .fromUriString("https://pho-te.com/workbook") + .build() + .toUriString() + } + + // 리다이렉트 및 쿠키 설정 + return ResponseEntity + .status(302) + .header(HttpHeaders.LOCATION, redirectUrl) + .header(HttpHeaders.SET_COOKIE, tokenCookie.toString()) + .build() } } From e35c4d8226b40f6c8667cf97947f99ac4bd7a298 Mon Sep 17 00:00:00 2001 From: adorableco Date: Thu, 8 Aug 2024 14:05:33 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=20Fix:=20getKaKaoUserInfo=20=EC=9B=90?= =?UTF-8?q?=EB=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit part of swm-147 related to: #168 --- .../phote/controller/AuthController.kt | 40 ++----------------- 1 file changed, 3 insertions(+), 37 deletions(-) diff --git a/src/main/kotlin/com/swm_standard/phote/controller/AuthController.kt b/src/main/kotlin/com/swm_standard/phote/controller/AuthController.kt index c74edfd..4cc26ef 100644 --- a/src/main/kotlin/com/swm_standard/phote/controller/AuthController.kt +++ b/src/main/kotlin/com/swm_standard/phote/controller/AuthController.kt @@ -7,15 +7,11 @@ import com.swm_standard.phote.service.KaKaoAuthService import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.tags.Tag import org.springframework.beans.factory.annotation.Value -import org.springframework.http.HttpHeaders -import org.springframework.http.ResponseCookie -import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController import org.springframework.web.servlet.view.RedirectView -import org.springframework.web.util.UriComponentsBuilder @RestController @RequestMapping("/api/auth") @@ -57,41 +53,11 @@ class AuthController( @GetMapping("/kakao-token") fun getKakaoUserInfo( @RequestParam code: String, - ): ResponseEntity { - // 카카오에서 액세스 토큰을 가져옵니다. + ): BaseResponse { val accessToken = kaKaoAuthService.getTokenFromKakao(code) - // 액세스 토큰을 사용하여 사용자 정보를 가져옵니다. val userInfo = kaKaoAuthService.getUserInfoFromKakao(accessToken) - // 쿠키에 액세스 토큰을 저장합니다. - val tokenCookie = - ResponseCookie - .from("accessToken", accessToken) - .httpOnly(true) - .secure(true) - .path("/") - .maxAge(7 * 24 * 60 * 60) // 7일 동안 유효 - .build() - - // 리다이렉트 URL 생성 - val redirectUrl = - if (userInfo.isMember == false) { - UriComponentsBuilder - .fromUriString("https://pho-te.com/workbook") - .build() - .toUriString() - } else { - UriComponentsBuilder - .fromUriString("https://pho-te.com/workbook") - .build() - .toUriString() - } - - // 리다이렉트 및 쿠키 설정 - return ResponseEntity - .status(302) - .header(HttpHeaders.LOCATION, redirectUrl) - .header(HttpHeaders.SET_COOKIE, tokenCookie.toString()) - .build() + val message = if (userInfo.isMember == false) "회원가입 성공" else "로그인 성공" + return BaseResponse(msg = message, data = userInfo) } }