Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

회원가입, 로그인 로직 수정 #91

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading