44import com .somemore .global .auth .jwt .domain .EncodedToken ;
55import com .somemore .global .auth .jwt .domain .TokenType ;
66import com .somemore .global .auth .jwt .usecase .GenerateTokensOnLoginUseCase ;
7+ import com .somemore .global .auth .oauth .domain .CustomOAuth2User ;
78import com .somemore .global .auth .oauth .processor .OAuthUserProcessor ;
89import com .somemore .global .auth .redirect .RedirectUseCase ;
910import com .somemore .user .domain .UserRole ;
@@ -35,7 +36,7 @@ public class CustomOAuthSuccessHandler extends SimpleUrlAuthenticationSuccessHan
3536 public void onAuthenticationSuccess (HttpServletRequest request ,
3637 HttpServletResponse response ,
3738 Authentication authentication ) {
38- OAuth2User oauthUser = extractOAuthUser (authentication );
39+ CustomOAuth2User oauthUser = extractOAuthUser (authentication );
3940 UUID userId = oauthUserProcessor .fetchUserIdByOAuthUser (oauthUser );
4041
4142 processAccessToken (response , userId );
@@ -55,14 +56,28 @@ private void processAccessToken(HttpServletResponse response, UUID userId) {
5556 generateTokensOnLoginUseCase .generateLoginToken (
5657 userId , UserRole .getOAuthUserDefaultRole ());
5758
58- cookieUseCase .setToken (response , loginToken .getValueWithPrefix (), TokenType .SIGN_IN );
59+ cookieUseCase .setToken (response , loginToken .value (), TokenType .SIGN_IN );
5960 }
6061
61- private OAuth2User extractOAuthUser (Authentication authentication ) {
62+ private CustomOAuth2User extractOAuthUser (Authentication authentication ) {
63+ OAuth2AuthenticationToken oAuth2AuthenticationToken = castToOAuth2AuthenticationTokenBy (authentication );
64+ OAuth2User oAuth2User = oAuth2AuthenticationToken .getPrincipal ();
65+ return castToCustomOAuth2UserBy (oAuth2User );
66+ }
67+
68+ private OAuth2AuthenticationToken castToOAuth2AuthenticationTokenBy (Authentication authentication ) {
6269 if (authentication instanceof OAuth2AuthenticationToken token ) {
63- return token . getPrincipal () ;
70+ return token ;
6471 }
6572 log .error ("Authentication 객체가 OAuth2AuthenticationToken 타입이 아닙니다: {}" , authentication .getClass ().getName ());
66- throw new IllegalArgumentException ("잘못된 인증 객체입니다." );
73+ throw new IllegalArgumentException ();
74+ }
75+
76+ private CustomOAuth2User castToCustomOAuth2UserBy (OAuth2User oAuth2User ) {
77+ if (oAuth2User instanceof CustomOAuth2User customOAuth2User ) {
78+ return customOAuth2User ;
79+ }
80+ log .error ("OAuth2User 객체가 CustomOAuth2User 타입이 아닙니다: {}" , oAuth2User .getClass ().getName ());
81+ throw new IllegalArgumentException ();
6782 }
6883}
0 commit comments