Skip to content

Commit

Permalink
fix: 유저코드 검증로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
SangWoon123 committed May 15, 2024
1 parent 01a1269 commit e9c4286
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import lombok.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

@Getter
@Entity
Expand Down Expand Up @@ -75,7 +77,7 @@ public void checkInvited(String userCode) throws BusinessException {
}

private String generateInviteCode() {
return "INV-" + LocalDateTime.now().toString();
return "INV"+UUID.randomUUID().toString();
}

public static GroupInviteResponse toInviteResponse(GroupInvite groupInvite) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class User extends BaseEntity {
@Column(name = "social_id")
private String socialId;

@Column(name = "user_code", nullable = false)
@Column(name = "user_code", nullable = false, unique = true)
private String userCode;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public interface UserRepository extends JpaRepository<User,Long> {
Optional<User> findByUserCode(String userCode);
Optional<User> findBySocialTypeAndSocialId(SocialType socialType, String socialId);
Optional<User> getUserById(Long userId);
boolean existsByUserCode(String userCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public User getUserProfile(Long userId) {
return userRepository.getUserById(userId)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
}

public boolean existsByUserCode(String userCode){
return userRepository.existsByUserCode(userCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class UserService {

private final UserQueryService userQueryService;
private final ScheduleQueryService scheduleQueryService;


@Transactional(readOnly = true)
public List<GroupRoom> findFavoriteGroupsByUserId(UserInfo userInfo) {
Expand All @@ -32,14 +32,16 @@ public List<GroupRoom> findFavoriteGroupsByUserId(UserInfo userInfo) {
.collect(Collectors.toList());
}

@Transactional
public User createUserFromRequest(AndroidLoginRequest androidLoginRequest) {
String userCode = generateUniqueUserCode();
User user = User.builder()
.socialId(androidLoginRequest.socialId())
.socialType(SocialType.KAKAO)
.username(androidLoginRequest.profileNickname())
.email(androidLoginRequest.accountEmail())
.profileImage(androidLoginRequest.profileImage())
.userCode(User.createCode())
.userCode(userCode)
.role(Role.USER)
.build();

Expand All @@ -60,4 +62,12 @@ public ProfileResponse getProfile(UserInfo userInfo) {
.role(Role.USER)
.build();
}

public String generateUniqueUserCode() {
String userCode;
do {
userCode = User.createCode();
} while (userQueryService.existsByUserCode(userCode));
return userCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.tukorea.planding.domain.user.repository.UserRepository;
import com.tukorea.planding.domain.user.entity.SocialType;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.domain.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
Expand All @@ -22,14 +23,15 @@
public class CustomOAuth2Service implements OAuth2UserService<OAuth2UserRequest, OAuth2User> {

private final UserRepository userRepository;
private final UserService userService;

@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
OAuth2UserService<OAuth2UserRequest, OAuth2User> delegate = new DefaultOAuth2UserService();
OAuth2User oAuth2User = delegate.loadUser(userRequest);

String registrationId=userRequest.getClientRegistration().getRegistrationId();
SocialType socialType=SocialType.getSocialType(registrationId);
String registrationId = userRequest.getClientRegistration().getRegistrationId();
SocialType socialType = SocialType.getSocialType(registrationId);
String userNameAttributeName = userRequest.getClientRegistration()
.getProviderDetails().getUserInfoEndpoint().getUserNameAttributeName(); // OAuth2 로그인 시 키(PK)가 되는 값

Expand All @@ -39,7 +41,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic

User createdUser = getUser(extractAttributes, socialType);

log.info("["+registrationId+"]:OAuth 객체 생성");
log.info("[" + registrationId + "]:OAuth 객체 생성");
return new CustomOAuth2User(
Collections.singleton(new SimpleGrantedAuthority(createdUser.getRole().getAuthority())),
attributes,
Expand All @@ -56,16 +58,19 @@ private User getUser(OAuthAttributes attributes, SocialType socialType) {
User findUser = userRepository.findBySocialTypeAndSocialId(socialType,
attributes.getOauth2UserInfo().getOAuth2Id()).orElse(null);

if(findUser == null) {
if (findUser == null) {
return saveUser(attributes, socialType);
}

log.info("기존 유저 유저 [userCode = " + findUser.getUserCode() + "]");
return findUser;
}

private User saveUser(OAuthAttributes attributes, SocialType socialType) {
log.info("신규 회원가입");
User createdUser = attributes.toEntity(socialType, attributes.getOauth2UserInfo());
String userCode = userService.generateUniqueUserCode();
User createdUser = attributes.toEntity(socialType, attributes.getOauth2UserInfo(), userCode);
return userRepository.save(createdUser);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ private static OAuthAttributes ofKakao(String userNameAttributeName, Map<String,
.build();
}

public User toEntity(SocialType socialType, OAuth2UserInfo oauth2UserInfo) {
public User toEntity(SocialType socialType, OAuth2UserInfo oauth2UserInfo, String userCode) {
return User.builder()
.socialType(socialType)
.socialId(oauth2UserInfo.getOAuth2Id())
.email(oauth2UserInfo.getEmail())
.username(oauth2UserInfo.getNickname())
.profileImage(oauth2UserInfo.getProfileImage())
.role(Role.USER)
.userCode(User.createCode())
.userCode(userCode)
.build();
}
}

0 comments on commit e9c4286

Please sign in to comment.