Skip to content

Commit

Permalink
[fix] 회원가입 관련 수정
Browse files Browse the repository at this point in the history
[fix] 회원가입 관련 수정
  • Loading branch information
bo-ram-bo-ram authored Jul 12, 2024
2 parents 054b3c7 + 58ff0d7 commit 3dea77d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public interface GuestRepository extends JpaRepository<Guest, Long> {

Guest findGuestByUserId(Long userId);

Optional<Guest> findByUserId(Long userId);

default Guest findGuestByIdOrThrow(Long id) {
return findGuestById(id)
.orElseThrow(() -> new CustomException(ErrorCode.GUEST_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.pickple.server.api.user.dto;

public record LoginSuccessResponse(
String accessToken,
String refreshToken
String guestNickname,
Long guestId,
String hostNickname,
Long hostId,
TokenDto token
) {
public static LoginSuccessResponse of(
final String accessToken,
final String refreshToken
final String guestNickname,
final Long guestId,
final String hostNickname,
final Long hostId,
final TokenDto token
) {
return new LoginSuccessResponse(accessToken, refreshToken);
return new LoginSuccessResponse(guestNickname, guestId, hostNickname, hostId, token);
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/pickple/server/api/user/dto/TokenDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.pickple.server.api.user.dto;

public record TokenDto(
String accessToken,
String refreshToken
) {

public static TokenDto of(String accessToken, String refreshToken) {
return new TokenDto(accessToken, refreshToken);
}
}
72 changes: 55 additions & 17 deletions src/main/java/com/pickple/server/api/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.pickple.server.api.user.service;

import com.pickple.server.api.guest.domain.Guest;
import com.pickple.server.api.guest.repository.GuestRepository;
import com.pickple.server.api.host.domain.Host;
import com.pickple.server.api.host.repository.HostRepository;
import com.pickple.server.api.user.domain.SocialType;
import com.pickple.server.api.user.domain.User;
import com.pickple.server.api.user.dto.AccessTokenGetSuccess;
import com.pickple.server.api.user.dto.LoginSuccessResponse;
import com.pickple.server.api.user.dto.TokenDto;
import com.pickple.server.api.user.repository.UserRepository;
import com.pickple.server.global.auth.client.dto.UserInfoResponse;
import com.pickple.server.global.auth.client.dto.UserLoginRequest;
Expand All @@ -24,12 +29,24 @@ public class UserService {
private final JwtTokenProvider jwtTokenProvider;
private final TokenService tokenService;
private final KakaoSocialService kakaoSocialService;
private final GuestRepository guestRepository; // 의존성 분리하기
private final HostRepository hostRepository; // 의존성 분리하기

public LoginSuccessResponse create(
final String authorizationCode,
final UserLoginRequest loginRequest
) {
return getTokenDto(getUserInfoResponse(authorizationCode, loginRequest));
User user = getUser(getUserInfoResponse(authorizationCode, loginRequest));
Guest guest = getOrCreateGuest(user);
TokenDto tokenDto = getTokenDto(user);

if (isExistingHost(user.getId())) {
Host host = hostRepository.findHostByUserId(user.getId());
return LoginSuccessResponse.of(guest.getNickname(), guest.getId(),
host.getNickname(), host.getId(), tokenDto);
} else {
return LoginSuccessResponse.of(guest.getNickname(), guest.getId(), null, null, tokenDto);
}
}

public UserInfoResponse getUserInfoResponse(
Expand All @@ -44,14 +61,23 @@ public UserInfoResponse getUserInfoResponse(
}
}

public Long createUser(final UserInfoResponse userResponse) {
public User createUser(final UserInfoResponse userResponse) {
User user = User.of(
userResponse.socialId(),
userResponse.email(),
userResponse.socialType(),
userResponse.socialNickname()
);
return userRepository.save(user).getId();
return userRepository.save(user);
}

public Guest createGuest(final User user) {
Guest guest = Guest.builder()
.user(user)
.nickname(user.getSocialNickname() + "#" + user.getId())
.imageUrl("testImage")
.build();
return guestRepository.save(guest);
}

public User getBySocialId(
Expand All @@ -77,20 +103,13 @@ public AccessTokenGetSuccess refreshToken(
);
}

public boolean isExistingUser(
final Long socialId,
final SocialType socialType
) {
return userRepository.findBySocialTypeAndSocialId(socialId, socialType).isPresent();
}

public LoginSuccessResponse getTokenByUserId(
public TokenDto getTokenByUserId(
final Long id
) {
UserAuthentication userAuthentication = new UserAuthentication(id, null, null);
String refreshToken = jwtTokenProvider.issueRefreshToken(userAuthentication);
tokenService.saveRefreshToken(id, refreshToken);
return LoginSuccessResponse.of(
return TokenDto.of(
jwtTokenProvider.issueAccessToken(userAuthentication),
refreshToken
);
Expand All @@ -107,14 +126,33 @@ public void deleteUser(
userRepository.delete(user);
}

private LoginSuccessResponse getTokenDto(
final UserInfoResponse userResponse
private TokenDto getTokenDto(
final User user
) {
return getTokenByUserId(user.getId());
}

private User getUser(final UserInfoResponse userResponse) {
if (isExistingUser(userResponse.socialId(), userResponse.socialType())) {
return getTokenByUserId(getBySocialId(userResponse.socialId(), userResponse.socialType()).getId());
return getBySocialId(userResponse.socialId(), userResponse.socialType());
} else {
Long id = createUser(userResponse);
return getTokenByUserId(id);
return createUser(userResponse);
}
}

private boolean isExistingUser(
final Long socialId,
final SocialType socialType
) {
return userRepository.findBySocialTypeAndSocialId(socialId, socialType).isPresent();
}

private boolean isExistingHost(final Long userId) {
return hostRepository.existsById(userId);
}

private Guest getOrCreateGuest(final User user) {
return guestRepository.findByUserId(user.getId())
.orElseGet(() -> createGuest(user));
}
}

0 comments on commit 3dea77d

Please sign in to comment.