diff --git a/src/main/java/com/groom/swipo/domain/auth/dto/request/KakaoLoginRequest.java b/src/main/java/com/groom/swipo/domain/auth/dto/request/KakaoLoginRequest.java deleted file mode 100644 index 6959d84..0000000 --- a/src/main/java/com/groom/swipo/domain/auth/dto/request/KakaoLoginRequest.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.groom.swipo.domain.auth.dto.request; - -public record KakaoLoginRequest( - String kakaoCode -) { -} diff --git a/src/main/java/com/groom/swipo/domain/auth/dto/request/AppleLoginRequest.java b/src/main/java/com/groom/swipo/domain/auth/dto/request/SocialLoginRequest.java similarity index 66% rename from src/main/java/com/groom/swipo/domain/auth/dto/request/AppleLoginRequest.java rename to src/main/java/com/groom/swipo/domain/auth/dto/request/SocialLoginRequest.java index 441fcae..95f78fc 100644 --- a/src/main/java/com/groom/swipo/domain/auth/dto/request/AppleLoginRequest.java +++ b/src/main/java/com/groom/swipo/domain/auth/dto/request/SocialLoginRequest.java @@ -1,6 +1,6 @@ package com.groom.swipo.domain.auth.dto.request; -public record AppleLoginRequest ( +public record SocialLoginRequest( String token ){ } diff --git a/src/main/java/com/groom/swipo/domain/auth/service/KakaoLoginService.java b/src/main/java/com/groom/swipo/domain/auth/service/KakaoLoginService.java index 22f5942..20b52f1 100644 --- a/src/main/java/com/groom/swipo/domain/auth/service/KakaoLoginService.java +++ b/src/main/java/com/groom/swipo/domain/auth/service/KakaoLoginService.java @@ -2,13 +2,11 @@ import java.net.URI; -import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.client.RestClient; -import org.springframework.web.util.UriComponentsBuilder; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; @@ -27,7 +25,6 @@ @RequiredArgsConstructor public class KakaoLoginService { - private static final String TOKEN_URL = "https://kauth.kakao.com/oauth/token"; private static final String USER_INFO_URL = "https://kapi.kakao.com/v2/user/me"; private final UserRepository userRepository; @@ -36,19 +33,9 @@ public class KakaoLoginService { private final RestClient restClient; private final ObjectMapper objectMapper; - @Value("${oauth.kakao.client-id}") - private String clientId; - - @Value("${oauth.kakao.client-secret}") - private String clientSecret; - - @Value("${oauth.kakao.redirect-uri}") - private String redirectUri; - @Transactional - public SocialLoginResponse kakaoLogin(String code) { + public SocialLoginResponse kakaoLogin(String kakaoAccessToken) { try { - String kakaoAccessToken = getKakaoAccessToken(code); String[] userInfo = getKakaoUserInfo(kakaoAccessToken); return userRepository.findByProviderAndProviderId(Provider.KAKAO, userInfo[0]) @@ -66,25 +53,6 @@ private SocialLoginResponse handleExistingUserLogin(User user) { return SocialLoginResponse.of(user.getId(), accessToken, refreshToken); } - private String getKakaoAccessToken(String code) throws JsonProcessingException { - String url = UriComponentsBuilder.fromHttpUrl(TOKEN_URL) - .queryParam("grant_type", "authorization_code") - .queryParam("client_id", clientId) - .queryParam("redirect_uri", redirectUri) - .queryParam("code", code) - .queryParam("client_secret", clientSecret) - .toUriString(); - - String response = restClient.post() - .uri(url) - .contentType(MediaType.APPLICATION_FORM_URLENCODED) - .retrieve() - .body(String.class); - - JsonNode jsonNode = objectMapper.readTree(response); - return jsonNode.get("access_token").asText(); - } - private String[] getKakaoUserInfo(String kakaoAccessToken) throws JsonProcessingException { HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", "Bearer " + kakaoAccessToken); @@ -102,17 +70,4 @@ private String[] getKakaoUserInfo(String kakaoAccessToken) throws JsonProcessing return new String[] {providerId, profileImageUrl}; } - - @Transactional - public SocialLoginResponse kakaoLoginWithAccessToken(String kakaoAccessToken) { - try { - String[] userInfo = getKakaoUserInfo(kakaoAccessToken); - - return userRepository.findByProviderAndProviderId(Provider.KAKAO, userInfo[0]) - .map(this::handleExistingUserLogin) - .orElseGet(() -> SocialLoginResponse.of(userInfo[0], userInfo[1])); - } catch (JsonProcessingException e) { - throw new KakaoAuthException(); - } - } } diff --git a/src/main/java/com/groom/swipo/domain/user/controller/UserController.java b/src/main/java/com/groom/swipo/domain/user/controller/UserController.java index 3a39f98..05496f7 100644 --- a/src/main/java/com/groom/swipo/domain/user/controller/UserController.java +++ b/src/main/java/com/groom/swipo/domain/user/controller/UserController.java @@ -10,8 +10,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import com.groom.swipo.domain.auth.dto.request.AppleLoginRequest; -import com.groom.swipo.domain.auth.dto.request.KakaoLoginRequest; +import com.groom.swipo.domain.auth.dto.request.SocialLoginRequest; import com.groom.swipo.domain.auth.dto.request.TokenRefreshRequest; import com.groom.swipo.domain.auth.dto.response.SocialLoginResponse; import com.groom.swipo.domain.auth.dto.response.TokenRefreshResponse; @@ -43,31 +42,10 @@ public class UserController { private final AppleLoginService appleLoginService; private final TokenRenewService tokenRenewService; - @PostMapping("/kakao/accessToken") - @Operation( - summary = "카카오 로그인", - description = "카카오 액세스 토큰을 사용하여 로그인 또는 회원가입 필요 여부를 판별합니다. DB에 사용자 정보가 있으면 로그인 성공, 없으면 회원가입 필요 상태를 반환합니다.", - security = {}, - responses = { - @ApiResponse(responseCode = "200", description = "로그인 성공"), - @ApiResponse(responseCode = "400", description = "잘못된 요청"), - @ApiResponse(responseCode = "401", description = "인증되지 않은 요청"), - @ApiResponse(responseCode = "418", description = "회원가입 필요"), - @ApiResponse(responseCode = "500", description = "서버 오류") - } - ) - public ResTemplate kakaoLoginWithAccessToken(@RequestBody KakaoLoginRequest request) { - SocialLoginResponse data = kakaoLoginService.kakaoLoginWithAccessToken(request.kakaoCode()); - if (data.userId() == null) { - return new ResTemplate<>(HttpStatus.I_AM_A_TEAPOT, "회원가입 필요", data); - } - return new ResTemplate<>(HttpStatus.OK, "로그인 성공", data); - } - @PostMapping("/kakao") @Operation( summary = "카카오 로그인", - description = "카카오 인가 코드를 사용하여 로그인 또는 회원가입 필요 여부를 판별합니다. DB에 사용자 정보가 있으면 로그인 성공, 없으면 회원가입 필요 상태를 반환합니다.", + description = "카카오 액세스 토큰을 사용하여 로그인 또는 회원가입 필요 여부를 판별합니다. DB에 사용자 정보가 있으면 로그인 성공, 없으면 회원가입 필요 상태를 반환합니다.", security = {}, responses = { @ApiResponse(responseCode = "200", description = "로그인 성공"), @@ -77,8 +55,8 @@ public ResTemplate kakaoLoginWithAccessToken(@RequestBody K @ApiResponse(responseCode = "500", description = "서버 오류") } ) - public ResTemplate kakaoLogin(@RequestBody KakaoLoginRequest request) { - SocialLoginResponse data = kakaoLoginService.kakaoLogin(request.kakaoCode()); + public ResTemplate kakaoLogin(@RequestBody SocialLoginRequest request) { + SocialLoginResponse data = kakaoLoginService.kakaoLogin(request.token()); if (data.userId() == null) { return new ResTemplate<>(HttpStatus.I_AM_A_TEAPOT, "회원가입 필요", data); } @@ -98,7 +76,7 @@ public ResTemplate kakaoLogin(@RequestBody KakaoLoginReques @ApiResponse(responseCode = "500", description = "서버 오류") } ) - public ResTemplate appleLogin(@RequestBody AppleLoginRequest request) { + public ResTemplate appleLogin(@RequestBody SocialLoginRequest request) { SocialLoginResponse data = appleLoginService.appleLogin(request.token()); if (data.userId() == null) { return new ResTemplate<>(HttpStatus.I_AM_A_TEAPOT, "회원가입 필요", data); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b91b737..2cf782e 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -33,15 +33,6 @@ jwt: access-token: ${jwt.expire-time.access-token} refresh-token: ${jwt.expire-time.refresh-token} -oauth: - kakao: - client-id: ${oauth.kakao.client-id} - client-secret: ${oauth.kakao.client-secret} - redirect-uri: ${oauth.kakao.redirect-uri} - - apple: - public-key-url: ${oauth.apple.public-key-url} - imp: api: key: ${imp.api.key}