-
Notifications
You must be signed in to change notification settings - Fork 2
MOSU-341 refactor: 카카오 회원 중복 시 에러 처리 #342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package life.mosu.mosuserver.global.exception; | ||
|
|
||
| import org.springframework.security.core.AuthenticationException; | ||
|
|
||
| public class OAuthException extends AuthenticationException { | ||
|
|
||
| public OAuthException(String msg) { | ||
| super(msg); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,6 @@ | |
| import jakarta.servlet.http.HttpServletResponse; | ||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import life.mosu.mosuserver.presentation.auth.dto.request.LoginResponse; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.security.core.AuthenticationException; | ||
|
|
@@ -27,8 +26,11 @@ public class OAuth2LoginFailureHandler implements | |
| public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, | ||
| AuthenticationException exception) throws IOException, ServletException { | ||
|
|
||
| LoginResponse loginResponse = LoginResponse.from(); | ||
| String jsonResponse = UriUtils.encode(objectMapper.writeValueAsString(loginResponse), | ||
| OAuthErrorType errorType = OAuthErrorType.from(exception.getMessage()); | ||
| OAuthFailureResponse oAuthFailureResponse = OAuthFailureResponse.from( | ||
| errorType.getMessage()); | ||
|
Comment on lines
+29
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
이 변경을 적용하려면
String errorCode = exception.getMessage();
if (exception instanceof org.springframework.security.oauth2.core.OAuth2AuthenticationException e) {
errorCode = e.getError().getErrorCode();
}
OAuthErrorType errorType = OAuthErrorType.from(errorCode);
OAuthFailureResponse oAuthFailureResponse = OAuthFailureResponse.from(
errorType.getMessage()); |
||
|
|
||
| String jsonResponse = UriUtils.encode(objectMapper.writeValueAsString(oAuthFailureResponse), | ||
| StandardCharsets.UTF_8); | ||
|
|
||
| final String redirectWithAccessToken = UriComponentsBuilder.fromUriString( | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,27 @@ | ||||||||||||||||||||||
| package life.mosu.mosuserver.global.handler; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import lombok.Getter; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @Getter | ||||||||||||||||||||||
| public enum OAuthErrorType { | ||||||||||||||||||||||
| CANCELED("CANCELED"), | ||||||||||||||||||||||
| DUPLICATE("DUPLICATE"), | ||||||||||||||||||||||
| UNKNOWN("UNKNOWN"); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| private final String message; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| OAuthErrorType(String message) { | ||||||||||||||||||||||
| this.message = message; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public static OAuthErrorType from(String text) { | ||||||||||||||||||||||
| if (text == null) { | ||||||||||||||||||||||
| return UNKNOWN; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| return switch (text) { | ||||||||||||||||||||||
| case "DUPLICATE" -> DUPLICATE; | ||||||||||||||||||||||
| case "[access_denied] User denied access" -> CANCELED; | ||||||||||||||||||||||
| default -> UNKNOWN; | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
Comment on lines
+21
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 에러 메시지 문자열
관련하여 아래와 같이 수정할 수 있습니다.
Suggested change
|
||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package life.mosu.mosuserver.global.handler; | ||
|
|
||
| public record OAuthFailureResponse( | ||
| Boolean isProfileRegistered, | ||
| String errorCode | ||
| ) { | ||
|
|
||
| public static OAuthFailureResponse from(String errorCode) { | ||
| return new OAuthFailureResponse(null, errorCode); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
에러 메시지로 하드코딩된 문자열
"DUPLICATE"을 사용하는 대신,OAuthErrorTypeenum을 직접 사용하여 타입 안정성을 높이고 유지보수를 용이하게 하는 것이 좋습니다.OAuthErrorType.DUPLICATE.getMessage()를 사용하면, 향후 에러 코드 문자열이 변경되더라도 한 곳(OAuthErrorTypeenum)에서만 수정하면 되므로 코드 관리가 더 수월해집니다. 이 변경을 적용하려면life.mosu.mosuserver.global.handler.OAuthErrorType를 import해야 합니다.