Skip to content

Commit

Permalink
Fix: Google Login 구현 변경
Browse files Browse the repository at this point in the history
part of swm-147
related to: #168
  • Loading branch information
adorableco committed Aug 8, 2024
1 parent 2dfdb4f commit ecd7c8f
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions src/main/kotlin/com/swm_standard/phote/controller/AuthController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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

Expand All @@ -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<UserInfoResponse> {
val accessToken = googleAuthService.getTokenFromGoogle(code)
Expand All @@ -70,11 +57,41 @@ class AuthController(
@GetMapping("/kakao-token")
fun getKakaoUserInfo(
@RequestParam code: String,
): BaseResponse<UserInfoResponse> {
): ResponseEntity<Void> {
// 카카오에서 액세스 토큰을 가져옵니다.
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()
}
}

0 comments on commit ecd7c8f

Please sign in to comment.