From f9cc4a24cba3d0eeb3b33497395185e8a703a576 Mon Sep 17 00:00:00 2001 From: Arachne <66822642+Arachneee@users.noreply.github.com> Date: Sat, 14 Dec 2024 15:11:11 +0900 Subject: [PATCH] =?UTF-8?q?[BE]=20=ED=9A=8C=EC=9B=90=20=EC=A0=95=EB=B3=B4?= =?UTF-8?q?=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= =?UTF-8?q?=20(#852)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 회원 정보 추가 기능 구현 * feat: 회원 프로필 추가 * fix: 회원/비회원 토큰 구분 불가 버그 수정 * feat: 프로필 없는 회원 구분 * feat: 엔드포인트 변경 --- .../event/application/EventService.java | 4 ++-- .../application/response/UserAppResponse.java | 8 ++++++-- .../domain/event/member/EventMember.java | 4 ++++ .../user/application/KakaoUserService.java | 9 +++++++-- .../user/application/UserService.java | 7 ++++--- .../request/UserJoinAppRequest.java | 13 +++++++++++++ .../main/java/haengdong/user/domain/User.java | 11 +++++++---- .../user/presentation/UserController.java | 10 +++++++++- .../presentation/response/UserResponse.java | 19 +++++++++++++++++++ 9 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 server/src/main/java/haengdong/user/application/request/UserJoinAppRequest.java create mode 100644 server/src/main/java/haengdong/user/presentation/response/UserResponse.java diff --git a/server/src/main/java/haengdong/event/application/EventService.java b/server/src/main/java/haengdong/event/application/EventService.java index 6633f390..104fc90b 100644 --- a/server/src/main/java/haengdong/event/application/EventService.java +++ b/server/src/main/java/haengdong/event/application/EventService.java @@ -53,7 +53,7 @@ public EventAppResponse saveEventGuest(EventGuestAppRequest request) { Event event = new Event(request.eventName(), userId, token); eventRepository.save(event); - eventMemberRepository.save(new EventMember(event, request.nickname())); + eventMemberRepository.save(EventMember.createHost(event, request.nickname())); return EventAppResponse.of(event); } @@ -64,7 +64,7 @@ public EventAppResponse saveEvent(EventAppRequest request) { eventRepository.save(event); String nickname = userService.findNicknameById(request.userId()); - eventMemberRepository.save(new EventMember(event, nickname)); + eventMemberRepository.save(EventMember.createHost(event, nickname)); return EventAppResponse.of(event); } diff --git a/server/src/main/java/haengdong/event/application/response/UserAppResponse.java b/server/src/main/java/haengdong/event/application/response/UserAppResponse.java index a29f765a..303e5065 100644 --- a/server/src/main/java/haengdong/event/application/response/UserAppResponse.java +++ b/server/src/main/java/haengdong/event/application/response/UserAppResponse.java @@ -3,11 +3,15 @@ import haengdong.user.domain.User; public record UserAppResponse( + String nickname, String bankName, String accountNumber, - Boolean isGuest + boolean isGuest, + String profileImage ) { public static UserAppResponse of(User user) { - return new UserAppResponse(user.getBank(), user.getAccountNumber(), user.isGuest()); + return new UserAppResponse(user.getNickname(), user.getBank(), user.getAccountNumber(), user.isGuest(), + user.getPicture() + ); } } diff --git a/server/src/main/java/haengdong/event/domain/event/member/EventMember.java b/server/src/main/java/haengdong/event/domain/event/member/EventMember.java index 379ee488..53e0c7c7 100644 --- a/server/src/main/java/haengdong/event/domain/event/member/EventMember.java +++ b/server/src/main/java/haengdong/event/domain/event/member/EventMember.java @@ -54,6 +54,10 @@ public EventMember(Long id, Event event, String name, boolean isDeposited) { this.isDeposited = isDeposited; } + public static EventMember createHost(Event event, String name) { + return new EventMember(null, event, name, true); + } + private void validateName(String name) { int nameLength = name.length(); if (nameLength < MIN_NAME_LENGTH || nameLength > MAX_NAME_LENGTH) { diff --git a/server/src/main/java/haengdong/user/application/KakaoUserService.java b/server/src/main/java/haengdong/user/application/KakaoUserService.java index 43119de7..b8be481e 100644 --- a/server/src/main/java/haengdong/user/application/KakaoUserService.java +++ b/server/src/main/java/haengdong/user/application/KakaoUserService.java @@ -1,7 +1,9 @@ package haengdong.user.application; import com.auth0.jwt.JWT; +import com.auth0.jwt.interfaces.Claim; import com.auth0.jwt.interfaces.DecodedJWT; +import haengdong.user.application.request.UserJoinAppRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -13,6 +15,7 @@ public class KakaoUserService { private static final String NICKNAME_KEY = "nickname"; + private static final String PICTURE_KEY = "picture"; private final UserService userService; private final KakaoClient kakaoClient; @@ -24,10 +27,12 @@ public Long joinByKakao(String code, String redirectUri) { String memberNumber = decodedJWT.getSubject(); String nickname = decodedJWT.getClaim(NICKNAME_KEY).asString(); + Claim pictureClaim = decodedJWT.getClaim(PICTURE_KEY); + String picture = pictureClaim.isNull() ? null : pictureClaim.asString(); - log.info("로그인 성공 : {}, {}, {}", code, memberNumber, nickname); + log.info("로그인 성공 : {}, {}, {}, {}", code, memberNumber, nickname, picture); - return userService.join(memberNumber, nickname); + return userService.join(new UserJoinAppRequest(memberNumber, nickname, picture)); } public String getClientId() { diff --git a/server/src/main/java/haengdong/user/application/UserService.java b/server/src/main/java/haengdong/user/application/UserService.java index 0e12f97c..0a87cf51 100644 --- a/server/src/main/java/haengdong/user/application/UserService.java +++ b/server/src/main/java/haengdong/user/application/UserService.java @@ -5,6 +5,7 @@ import haengdong.common.exception.HaengdongException; import haengdong.event.application.response.UserAppResponse; import haengdong.user.application.request.UserGuestSaveAppRequest; +import haengdong.user.application.request.UserJoinAppRequest; import haengdong.user.application.request.UserUpdateAppRequest; import haengdong.user.domain.User; import haengdong.user.domain.repository.UserRepository; @@ -28,9 +29,9 @@ public Long joinGuest(UserGuestSaveAppRequest request) { } @Transactional - public Long join(String memberNumber, String nickname) { - User user = userRepository.findByMemberNumber(memberNumber) - .orElseGet(() -> userRepository.save(User.createMember(nickname, memberNumber))); + public Long join(UserJoinAppRequest request) { + User user = userRepository.findByMemberNumber(request.memberNumber()) + .orElseGet(() -> userRepository.save(request.toUser())); return user.getId(); } diff --git a/server/src/main/java/haengdong/user/application/request/UserJoinAppRequest.java b/server/src/main/java/haengdong/user/application/request/UserJoinAppRequest.java new file mode 100644 index 00000000..6862ca2a --- /dev/null +++ b/server/src/main/java/haengdong/user/application/request/UserJoinAppRequest.java @@ -0,0 +1,13 @@ +package haengdong.user.application.request; + +import haengdong.user.domain.User; + +public record UserJoinAppRequest( + String memberNumber, + String nickname, + String picture +) { + public User toUser() { + return User.createMember(nickname, memberNumber, picture); + } +} diff --git a/server/src/main/java/haengdong/user/domain/User.java b/server/src/main/java/haengdong/user/domain/User.java index a8c04edd..2b662b53 100644 --- a/server/src/main/java/haengdong/user/domain/User.java +++ b/server/src/main/java/haengdong/user/domain/User.java @@ -46,20 +46,23 @@ public class User extends BaseEntity { @Column(unique = true) private String memberNumber; - private User(String nickname, String password, String bank, String accountNumber, String memberNumber) { + private String picture; + + private User(String nickname, String password, String bank, String accountNumber, String memberNumber, String picture) { this.nickname = nickname; this.password = new Password(password); this.bank = bank; this.accountNumber = accountNumber; this.memberNumber = memberNumber; + this.picture = picture; } public static User createGuest(String nickName, String password) { - return new User(nickName, password, "", "", null); + return new User(nickName, password, "", "", null, null); } - public static User createMember(String nickName, String memberNumber) { - return new User(nickName, "0000", "", "", memberNumber); + public static User createMember(String nickName, String memberNumber, String picture) { + return new User(nickName, "0000", "", "", memberNumber, picture); } public void changeNickname(String nickname) { diff --git a/server/src/main/java/haengdong/user/presentation/UserController.java b/server/src/main/java/haengdong/user/presentation/UserController.java index 76c418f4..c21c4beb 100644 --- a/server/src/main/java/haengdong/user/presentation/UserController.java +++ b/server/src/main/java/haengdong/user/presentation/UserController.java @@ -1,5 +1,7 @@ package haengdong.user.presentation; +import haengdong.event.application.response.UserAppResponse; +import haengdong.user.presentation.response.UserResponse; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -56,7 +58,7 @@ public ResponseEntity kakaoLogin( ) { log.info("Kakao login code, redirectUri: {}, {}", code, redirectUri); Long userId = kakaoUserService.joinByKakao(code, redirectUri); - String jwtToken = authService.createGuestToken(userId); + String jwtToken = authService.createMemberToken(userId); ResponseCookie responseCookie = createResponseCookie(jwtToken); return ResponseEntity.ok() @@ -64,6 +66,12 @@ public ResponseEntity kakaoLogin( .build(); } + @GetMapping("/api/users/mine") + public ResponseEntity findUser(@Login Long userId) { + UserAppResponse response = userService.findById(userId); + return ResponseEntity.ok(UserResponse.of(response)); + } + @DeleteMapping("/api/users") public ResponseEntity deleteUser(@Login Long userId) { userService.withdraw(userId); diff --git a/server/src/main/java/haengdong/user/presentation/response/UserResponse.java b/server/src/main/java/haengdong/user/presentation/response/UserResponse.java new file mode 100644 index 00000000..5260f5d8 --- /dev/null +++ b/server/src/main/java/haengdong/user/presentation/response/UserResponse.java @@ -0,0 +1,19 @@ +package haengdong.user.presentation.response; + +import haengdong.event.application.response.UserAppResponse; + +public record UserResponse( + String nickname, + String bankName, + String accountNumber, + boolean isGuest, + String profileImage +) { + + public static UserResponse of(UserAppResponse response) { + return new UserResponse( + response.nickname(), response.bankName(), response.accountNumber(), response.isGuest(), + response.profileImage() + ); + } +}