Skip to content

Commit

Permalink
Merge pull request #91 from billbill-project/feature/borrowPosts
Browse files Browse the repository at this point in the history
회원가입, 로그인 로직 수정
  • Loading branch information
ksj000625 authored Dec 22, 2024
2 parents 7717c56 + 0c4fd39 commit 9b76897
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@AllArgsConstructor
public class UserBaseInfo {
private String userId;
private String email;
private String profileImage;
private String nickname;
private String password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

@Data
public class LoginRequest {
@Schema(description = "전화번호", example = "010-0000-0001")
private String phoneNumber;
// @Schema(description = "전화번호", example = "010-0000-0001")
// private String phoneNumber;
@Schema(description = "이메일", example = "[email protected]")
private String email;
@Schema(description = "비밀번호", example = "password")
private String password;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
public class SignupRequest {
@Schema(description = "회원 프로필 이미지 URL", example = "profile image url")
private String profileImage;
@Schema(description = "이메일", example = "[email protected]")
private String email;
@Schema(description = "회원 닉네임", example = "nickname")
private String nickname;
@Schema(description = "비밀번호", example = "password")
private String password;
@Schema(description = "회원 본인인증 정보")
private IdentityRequest identity;
// @Schema(description = "회원 본인인증 정보")
// private IdentityRequest identity;
@Schema(description = "각종 약관 동의")
private AgreeRequest agree;
@Schema(description = "위치 정보")
private LocationRequest location;
// @Schema(description = "위치 정보")
// private LocationRequest location;
@Schema(description = "디바이스 정보")
private DeviceRequest device;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import site.billbill.apiserver.model.user.*;
import site.billbill.apiserver.repository.user.*;

import javax.swing.text.html.Option;
import java.util.Optional;

@Slf4j
Expand All @@ -46,54 +47,51 @@ public class AuthServiceImpl implements AuthService {
@Transactional
public JwtDto signup(SignupRequest request) {
// Check new by name & phoneNumber
Optional<UserIdentityJpaEntity> identityJpaEntity = userIdentityRepository.findUserByPhoneNumberWithoutWithdraw(request.getIdentity().getPhoneNumber());
// Optional<UserIdentityJpaEntity> identityJpaEntity = userIdentityRepository.findUserByPhoneNumberWithoutWithdraw(request.getIdentity().getPhoneNumber());

Optional<UserJpaEntity> userAlready = userRepository.findByEmailWithoutWithdraw(request.getEmail());

// if user already exists
if (identityJpaEntity.isPresent()) {
if (userAlready.isPresent())
throw new CustomException(ErrorCode.Conflict, "이미 존재하는 회원입니다.", HttpStatus.CONFLICT);
}

String encryptedPassword = bCryptPasswordEncoder.encode(request.getPassword());

// if user new
String userId = ULIDUtil.generatorULID("USER");
UserIdentityJpaEntity userIdentity = UserIdentityJpaEntity.toJpaEntity(userId, request.getIdentity());
UserJpaEntity user = new UserJpaEntity(new UserBaseInfo(userId, request.getProfileImage(), request.getNickname(), encryptedPassword));
// UserIdentityJpaEntity userIdentity = UserIdentityJpaEntity.toJpaEntity(userId, request.getIdentity());
UserJpaEntity user = new UserJpaEntity(new UserBaseInfo(userId, request.getEmail(), request.getProfileImage(), request.getNickname(), encryptedPassword));
UserAgreeHistJpaEntity userAgree = new UserAgreeHistJpaEntity(userId, request.getAgree().isServiceAgree(), request.getAgree().isPrivacyAgree(), request.getAgree().isMarketingAgree(), request.getAgree().isThirdPartyAgree());
UserDeviceJpaEntity userDevice = new UserDeviceJpaEntity(userId, ULIDUtil.generatorULID("DEVICE"), request.getDevice().getDeviceToken(), request.getDevice().getDeviceType(), request.getDevice().getAppVersion());

// save new user
userRepository.save(user);
userIdentityRepository.save(userIdentity);
// userIdentityRepository.save(userIdentity);
userDeviceRepository.save(userDevice);
userAgreeHistRepository.save(userAgree);

// save location
userService.saveLocation(userId, request.getLocation());
// userService.saveLocation(userId, request.getLocation());

return jwtUtil.generateJwtDto(userId, UserRole.USER);
}

@Override
public JwtDto login(LoginRequest request) {
// bring user's phone number
Optional<UserIdentityJpaEntity> userIdentityJpaEntity = userIdentityRepository.findUserByPhoneNumberWithoutWithdraw(request.getPhoneNumber());
// Optional<UserIdentityJpaEntity> userIdentityJpaEntity = userIdentityRepository.findUserByPhoneNumberWithoutWithdraw(request.getPhoneNumber());

if (userIdentityJpaEntity.isEmpty())
throw new CustomException(ErrorCode.NotFound, "전화번호를 확인해주세요", HttpStatus.NOT_FOUND);
Optional<UserJpaEntity> user = userRepository.findByEmailWithoutWithdraw(request.getEmail());

// bring user's password
String userId = userIdentityJpaEntity.get().getUserId();
Optional<UserJpaEntity> userJpaEntity = userRepository.findById(userId);
if (userJpaEntity.isEmpty())
throw new CustomException(ErrorCode.NotFound, "해당 회원이 존재하지 않습니다.", HttpStatus.NOT_FOUND);
// if user already exists
if (user.isEmpty())
throw new CustomException(ErrorCode.Conflict, "해당 회원이 존재하지 않습니다.", HttpStatus.CONFLICT);

String encryptedPassword = userJpaEntity.get().getPassword();
String encryptedPassword = user.get().getPassword();
if (!checkPassword(request.getPassword(), encryptedPassword))
throw new CustomException(ErrorCode.Unauthorized, "비밀번호를 확인해 주세요.", HttpStatus.UNAUTHORIZED);


return jwtUtil.generateJwtDto(userId, userJpaEntity.get().getRole());

return jwtUtil.generateJwtDto(user.get().getUserId(), user.get().getRole());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class UserJpaEntity extends BaseTime {

public UserJpaEntity(UserBaseInfo info) {
this.userId = info.getUserId();
this.email = info.getEmail();
this.nickname = info.getNickname();
this.profile = info.getProfileImage();
this.password = info.getPassword();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package site.billbill.apiserver.repository.user;

import site.billbill.apiserver.api.users.dto.request.ProfileRequest;
import site.billbill.apiserver.model.user.UserJpaEntity;

import java.util.Optional;

public interface UserDslRepository {
void withdrawUserById(String userId);

void updateProfileById(String userId, ProfileRequest request);

Optional<UserJpaEntity> findByEmailWithoutWithdraw(String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import org.springframework.stereotype.Repository;
import site.billbill.apiserver.api.users.dto.request.ProfileRequest;
import site.billbill.apiserver.model.user.QUserJpaEntity;
import site.billbill.apiserver.model.user.UserJpaEntity;

import java.time.OffsetDateTime;
import java.util.Optional;

@Repository
@RequiredArgsConstructor
Expand Down Expand Up @@ -40,4 +42,14 @@ public void updateProfileById(String userId, ProfileRequest request) {

qb.execute();
}

@Override
public Optional<UserJpaEntity> findByEmailWithoutWithdraw(String email) {
QUserJpaEntity qUser = QUserJpaEntity.userJpaEntity;

JPAQuery<UserJpaEntity> qb = query.selectFrom(qUser)
.where(qUser.email.eq(email).and(qUser.withdrawStatus.isFalse()));

return Optional.ofNullable(qb.fetchOne());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
import java.util.Optional;

public interface UserIdentityDslRepository {
public Optional<UserIdentityJpaEntity> findUserByPhoneNumberWithoutWithdraw(String phoneNumber);
Optional<UserIdentityJpaEntity> findUserByPhoneNumberWithoutWithdraw(String phoneNumber);
}

0 comments on commit 9b76897

Please sign in to comment.