Skip to content

Commit d58eced

Browse files
committed
refactor: 함수로 분리
1 parent 0ddecb7 commit d58eced

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

src/main/java/com/example/log4u/common/oauth2/service/CustomOAuth2UserService.java

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import com.example.log4u.common.oauth2.dto.CustomOAuth2User;
1212
import com.example.log4u.common.oauth2.dto.GoogleResponseDto;
13+
import com.example.log4u.common.oauth2.dto.KakaoResponseDto;
1314
import com.example.log4u.common.oauth2.dto.NaverResponseDto;
1415
import com.example.log4u.common.oauth2.dto.OAuth2Response;
1516
import com.example.log4u.common.oauth2.dto.UserCreateRequestDto;
@@ -36,48 +37,52 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
3637
String registrationId = userRequest.getClientRegistration().getRegistrationId();
3738

3839
// 정보 가공
39-
OAuth2Response oAuth2Response = switch(registrationId) {
40+
OAuth2Response oAuth2Response = switch (registrationId) {
4041
case "naver" -> new NaverResponseDto(oAuth2User.getAttributes());
4142
case "google" -> new GoogleResponseDto(oAuth2User.getAttributes());
43+
case "kakao" -> new KakaoResponseDto(oAuth2User.getAttributes());
4244
default -> throw new OAuth2AuthenticationException("지원하지 않는 소셜 로그인");
4345
};
4446

4547
// 정보 조회
4648
String providerId = oAuth2Response.getProviderId();
4749
Optional<User> dbUser = userRepository.findByProviderId(providerId);
4850

49-
UserCreateRequestDto userCreateRequestDto;
50-
// 첫 로그이면 이면 프로필 없으므로 우선 GUEST 설정
51+
// 첫 로그인이면 프로필 없으므로 우선 GUEST 설정
5152
if (dbUser.isEmpty()) {
52-
userCreateRequestDto = UserCreateRequestDto.fromOAuth2Response(
53-
oAuth2Response,
54-
null,
55-
"ROLE_GUEST"
56-
);
57-
User user = UserCreateRequestDto.toEntity(userCreateRequestDto);
58-
userRepository.save(user);
53+
return createUser(oAuth2Response);
54+
} else { // DB의 유저 정보 갱신
55+
return updateUser(oAuth2Response, dbUser.get());
56+
}
57+
}
5958

59+
public CustomOAuth2User createUser(OAuth2Response oAuth2Response) {
60+
UserCreateRequestDto userCreateRequestDto = UserCreateRequestDto.fromOAuth2Response(
61+
oAuth2Response,
62+
null,
63+
"ROLE_GUEST"
64+
);
65+
User user = UserCreateRequestDto.toEntity(userCreateRequestDto);
66+
userRepository.save(user);
6067

61-
UserCreateRequestDto afterSaveDto = UserCreateRequestDto.fromOAuth2Response(
62-
oAuth2Response,
63-
user.getUserId(),
64-
"ROLE_GUEST"
65-
);
68+
UserCreateRequestDto afterSaveDto = UserCreateRequestDto.fromOAuth2Response(
69+
oAuth2Response,
70+
user.getUserId(),
71+
"ROLE_GUEST"
72+
);
6673

67-
return new CustomOAuth2User(afterSaveDto);
68-
}
69-
// DB의 유저 정보 갱신
70-
else {
71-
User user = dbUser.get();
72-
user.updateOauth2Profile(oAuth2Response);
73-
userRepository.save(user);
74+
return new CustomOAuth2User(afterSaveDto);
75+
}
7476

75-
userCreateRequestDto = UserCreateRequestDto.fromOAuth2Response(
76-
oAuth2Response,
77-
user.getUserId(),
78-
user.getRole()
79-
);
80-
return new CustomOAuth2User(userCreateRequestDto);
81-
}
77+
public CustomOAuth2User updateUser(OAuth2Response oAuth2Response, User user) {
78+
user.updateOauth2Profile(oAuth2Response);
79+
userRepository.save(user);
80+
81+
UserCreateRequestDto userCreateRequestDto = UserCreateRequestDto.fromOAuth2Response(
82+
oAuth2Response,
83+
user.getUserId(),
84+
user.getRole()
85+
);
86+
return new CustomOAuth2User(userCreateRequestDto);
8287
}
8388
}

0 commit comments

Comments
 (0)