1010import io .f1 .backend .domain .user .dto .SignupResponseDto ;
1111import io .f1 .backend .domain .user .entity .User ;
1212import io .f1 .backend .global .util .SecurityUtils ;
13-
1413import jakarta .servlet .http .HttpSession ;
15-
1614import lombok .RequiredArgsConstructor ;
17-
1815import org .springframework .stereotype .Service ;
1916import org .springframework .transaction .annotation .Transactional ;
2017
@@ -32,7 +29,7 @@ public SignupResponseDto signup(HttpSession session, SignupRequestDto signupRequ
3229 validateNicknameFormat (nickname );
3330 validateNicknameDuplicate (nickname );
3431
35- User user = updateUserNickname (authenticationUser .userId (), nickname );
32+ User user = initNickname (authenticationUser .userId (), nickname );
3633 updateSessionAfterSignup (session , user );
3734 SecurityUtils .setAuthentication (user );
3835
@@ -41,7 +38,7 @@ public SignupResponseDto signup(HttpSession session, SignupRequestDto signupRequ
4138
4239 private AuthenticationUser extractSessionUser (HttpSession session ) {
4340 AuthenticationUser authenticationUser =
44- (AuthenticationUser ) session .getAttribute (OAUTH_USER );
41+ (AuthenticationUser ) session .getAttribute (OAUTH_USER );
4542 if (authenticationUser == null ) {
4643 throw new RuntimeException ("E401001: 로그인이 필요합니다." );
4744 }
@@ -68,11 +65,11 @@ public void validateNicknameDuplicate(String nickname) {
6865 }
6966
7067 @ Transactional
71- public User updateUserNickname (Long userId , String nickname ) {
68+ public User initNickname (Long userId , String nickname ) {
7269 User user =
73- userRepository
74- .findById (userId )
75- .orElseThrow (() -> new RuntimeException ("E404001: 존재하지 않는 회원입니다." ));
70+ userRepository
71+ .findById (userId )
72+ .orElseThrow (() -> new RuntimeException ("E404001: 존재하지 않는 회원입니다." ));
7673 user .updateNickname (nickname );
7774
7875 return userRepository .save (user );
@@ -89,4 +86,14 @@ public void deleteUser(Long userId) {
8986 .orElseThrow (() -> new RuntimeException ("E404001: 존재하지 않는 회원입니다." ));
9087 userRepository .delete (user );
9188 }
89+
90+ @ Transactional
91+ public void updateNickname (Long userId , String newNickname , HttpSession session ) {
92+ validateNicknameFormat (newNickname );
93+ validateNicknameDuplicate (newNickname );
94+
95+ User user = initNickname (userId , newNickname );
96+ session .setAttribute (USER , AuthenticationUser .from (user ));
97+ SecurityUtils .setAuthentication (user );
98+ }
9299}
0 commit comments