66import com .blockcloud .dto .oauth .GoogleResponse ;
77import com .blockcloud .dto .oauth .OAuth2ResponseDto ;
88import lombok .AllArgsConstructor ;
9+ import lombok .extern .slf4j .Slf4j ;
910import org .springframework .security .oauth2 .client .userinfo .DefaultOAuth2UserService ;
1011import org .springframework .security .oauth2 .client .userinfo .OAuth2UserRequest ;
1112import org .springframework .security .oauth2 .core .OAuth2AuthenticationException ;
13+ import org .springframework .security .oauth2 .core .OAuth2Error ;
1214import org .springframework .security .oauth2 .core .user .OAuth2User ;
1315import org .springframework .stereotype .Service ;
1416
1517@ Service
1618@ AllArgsConstructor
19+ @ Slf4j
1720public class CustomOAuth2UserService extends DefaultOAuth2UserService {
18- private final UserRepository userRepository ;
19-
20- @ Override
21- public OAuth2User loadUser (OAuth2UserRequest userRequest ) throws OAuth2AuthenticationException {
22- OAuth2User oAuth2User = super .loadUser (userRequest );
23-
24- String registrationId = userRequest .getClientRegistration ().getRegistrationId ();
25- OAuth2ResponseDto oAuth2Response = null ;
26-
27- if (registrationId .equals ("google" )) {
28- oAuth2Response = new GoogleResponse (oAuth2User .getAttributes ());
29- } else {
30- return null ;
31- }
32-
33- String email = oAuth2Response .getEmail ();
34- User existData = userRepository .findByEmail (email ).orElse (null );
35-
36- if (existData == null ) {
37- User user = User .builder ()
38- .email (oAuth2Response .getEmail ())
39- .username (oAuth2Response .getName ())
40- .imgUrl (oAuth2Response .getPicture ()) //
41- .role ("ROLE_USER" ) // 기본 역할 설정
42- .build ();
43-
44- userRepository .save (user );
45-
46- return new CustomOAuth2User (user );
47- } else {
48- return new CustomOAuth2User (existData );
49- }
50- }
51- }
21+ private final UserRepository userRepository ;
22+
23+ @ Override
24+ public OAuth2User loadUser (OAuth2UserRequest userRequest ) throws OAuth2AuthenticationException {
25+
26+ try {
27+ OAuth2User oAuth2User = super .loadUser (userRequest );
28+ log .info ("Google OAuth2 Attributes: {}" , oAuth2User .getAttributes ());
29+
30+ String registrationId = userRequest .getClientRegistration ().getRegistrationId ();
31+ OAuth2ResponseDto oAuth2Response = null ;
32+
33+ if (registrationId .equals ("google" )) {
34+ oAuth2Response = new GoogleResponse (oAuth2User .getAttributes ());
35+ log .info ("Mapped GoogleResponse Email: {}" , oAuth2Response .getEmail ());
36+ } else {
37+ log .warn ("Unsupported registrationId: {}" , registrationId );
38+ throw new OAuth2AuthenticationException ("Unsupported provider: " + registrationId );
39+ }
40+
41+ String email = oAuth2Response .getEmail ();
42+ User existData = userRepository .findByEmail (email ).orElse (null );
43+
44+ if (existData == null ) {
45+ log .info ("New user found. Email: {}" , email );
46+ User user = User .builder ()
47+ .email (oAuth2Response .getEmail ())
48+ .username (oAuth2Response .getName ())
49+ .imgUrl (oAuth2Response .getPicture ())
50+ .role ("ROLE_USER" )
51+ .build ();
52+
53+ userRepository .save (user );
54+ log .info ("New user saved successfully. ID: {}" , user .getId ());
55+
56+ return new CustomOAuth2User (user );
57+ } else {
58+ log .info ("Existing user found. Email: {}" , email );
59+ return new CustomOAuth2User (existData );
60+ }
61+ } catch (OAuth2AuthenticationException e ) {
62+ log .error ("OAuth2AuthenticationException occurred during user loading: {}" , e .getMessage ());
63+ throw e ;
64+ } catch (Exception e ) {
65+ log .error ("Critical error in CustomOAuth2UserService: Failed to process user data." , e );
66+ OAuth2Error oauth2Error = new OAuth2Error (
67+ "internal_server_error" ,
68+ "Internal server error during OAuth2 login process: " + e .getMessage (),
69+ null
70+ );
71+ throw new OAuth2AuthenticationException (oauth2Error , e );
72+ }
73+ }
74+ }
0 commit comments