Skip to content

Commit

Permalink
[BE] 회원 정보 조회 기능 구현 (#852)
Browse files Browse the repository at this point in the history
* feat: 회원 정보 추가 기능 구현

* feat: 회원 프로필 추가

* fix: 회원/비회원 토큰 구분 불가 버그 수정

* feat: 프로필 없는 회원 구분

* feat: 엔드포인트 변경
  • Loading branch information
Arachneee authored Dec 14, 2024
1 parent bf40131 commit f9cc4a2
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
11 changes: 7 additions & 4 deletions server/src/main/java/haengdong/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -56,14 +58,20 @@ public ResponseEntity<Void> 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()
.header(HttpHeaders.SET_COOKIE, responseCookie.toString())
.build();
}

@GetMapping("/api/users/mine")
public ResponseEntity<UserResponse> findUser(@Login Long userId) {
UserAppResponse response = userService.findById(userId);
return ResponseEntity.ok(UserResponse.of(response));
}

@DeleteMapping("/api/users")
public ResponseEntity<Void> deleteUser(@Login Long userId) {
userService.withdraw(userId);
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
);
}
}

0 comments on commit f9cc4a2

Please sign in to comment.