Skip to content

Commit b302c63

Browse files
authored
fix : 구글 소셜 로그인 문제되었던 부분들 수정 #13 (#14)
1 parent 5838d4a commit b302c63

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

src/main/java/com/backend/crame/domain/google/controller/GoogleController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import reactor.core.publisher.Mono;
1616

1717
@RestController
18-
@RequestMapping("/api/v1/google")
18+
@RequestMapping("/api/v1/auth/google")
1919
@Tag(name = "구글 로그인 API", description = "구글 로그인 관련 API입니다.")
2020
@RequiredArgsConstructor
2121
public class GoogleController {

src/main/java/com/backend/crame/domain/google/service/GoogleOAuthService.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88

99
import org.springframework.beans.factory.annotation.Value;
1010
import org.springframework.core.ParameterizedTypeReference;
11+
import org.springframework.http.HttpStatusCode;
12+
import org.springframework.http.MediaType;
1113
import org.springframework.stereotype.Service;
14+
import org.springframework.util.LinkedMultiValueMap;
15+
import org.springframework.util.MultiValueMap;
16+
import org.springframework.web.reactive.function.BodyInserters;
1217
import org.springframework.web.reactive.function.client.WebClient;
1318

1419
import com.backend.crame.domain.user.entitiy.Domain;
@@ -57,24 +62,25 @@ private Mono<String> exchangeCodeForAccessToken(String code) {
5762

5863
return webClient.post()
5964
.uri("https://oauth2.googleapis.com/token")
60-
.bodyValue(Map.of(
61-
"code", decodedCode,
62-
"client_id", clientId,
63-
"client_secret", clientSecret,
64-
"redirect_uri", redirectUri,
65-
"grant_type", "authorization_code"
66-
))
65+
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
66+
.body(BodyInserters.fromFormData("grant_type", "authorization_code")
67+
.with("client_id", clientId)
68+
.with("client_secret", clientSecret)
69+
.with("redirect_uri", redirectUri)
70+
.with("code", decodedCode))
6771
.retrieve()
68-
.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {})
72+
.bodyToMono(Map.class)
6973
.map(body -> {
70-
if (!body.containsKey("access_token")) {
71-
throw new BaseException(ErrorCode.LOGIN_FAIL);
74+
String token = (String)body.get("access_token");
75+
if (token == null) {
76+
throw new BaseException(ErrorCode.GOOGLE_LOGIN_FAIL);
7277
}
73-
return (String) body.get("access_token");
74-
});
75-
78+
return token;
79+
})
80+
.onErrorMap(e -> new BaseException(ErrorCode.GOOGLE_LOGIN_FAIL));
7681
}
7782

83+
7884
private Mono<Map<String, Object>> fetchUserInfo(String accessToken) {
7985
return webClient.get()
8086
.uri("https://www.googleapis.com/oauth2/v3/userinfo")

src/main/java/com/backend/crame/domain/user/utils/BirthdateParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static LocalDate parseToLocalDate(String input) {
1010
// 숫자만 추출
1111
String digits = input.replaceAll("\\D", "");
1212
if (digits.length() < 7) {
13-
throw new IllegalArgumentException("형식 오류: 최소 7자리(YYMMDD + 코드)가 필요합니다.");
13+
throw new IllegalArgumentException("형식 오류: 최소 7자리(YYMMDD + 뒷자리 한자리)가 필요합니다.");
1414
}
1515

1616
String yymmdd = digits.substring(0, 6);

src/main/java/com/backend/crame/global/exception/ErrorCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@Getter
1515
public enum ErrorCode {
1616

17-
17+
GOOGLE_LOGIN_FAIL(BAD_REQUEST,404,"구글 로그인에 실패하였습니다."),
1818
LOGIN_FAIL(HttpStatus.BAD_REQUEST,400,"로그인에 오류가 발생하였습니다."),
1919
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR,500,"서버에 오류가 발생하였습니다."),
2020
JWT_KEY_GENERATION_FAILED(HttpStatus.BAD_REQUEST,400,"JWT 키 생성에 실패하였습니다."),

0 commit comments

Comments
 (0)