Skip to content

Commit

Permalink
Refactor : 로그인 로직 수정 (#187)
Browse files Browse the repository at this point in the history
* Refactor : 로그인 로직 수정

최초 로그인 시에 inactive 상태 부여
이후 이름,성별,나이가 입력되면 active 상태로 전환

* Feat : 유저 성별 없을때 디폴트 이미지로 프로필 설정
  • Loading branch information
Astin01 authored Sep 21, 2024
1 parent 8f3234c commit 1c71e26
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public class OauthService {
private String USER_PROFILE_MALE;
@Value("${user.profile.url.female}")
private String USER_PROFILE_FEMALE;


@Value("${user.profile.url.none}")
private String USER_PROFILE_NONE;

public OauthLinkResponse generateAuthUrl(String type, String redirectUrl) {
String oauthLink = getAuthLink(type, redirectUrl);
Expand Down Expand Up @@ -120,7 +120,7 @@ private User checkAndSaveUser(String type, String code, String redirectUrl) {
throw new RuntimeException("지원하지 않는 oauth 타입입니다.");
}
}

private void checkUserStatus(User user) {
UserStatus userStatus = user.getUserStatus();
switch (userStatus){
Expand All @@ -144,7 +144,7 @@ private User saveGoogleUser(GoogleUserResponse response) {
UserImage savedUserImage = userImageService.saveUserImage(imageUrl);

User user = User.builder()
.userStatus(UserStatus.ACTIVATE)
.userStatus(UserStatus.INACTIVATE)
.oauthId(response.getResourceName())
.provider("google")
.isAdmin(false)
Expand All @@ -167,7 +167,7 @@ private String getGoogleUserImage(GoogleUserResponse response) {
if (Objects.equals(gender, "female")) {
return USER_PROFILE_FEMALE;
}
return "none";
return USER_PROFILE_NONE;
}

private User saveKakaoUser(KakaoUserResponse response) {
Expand All @@ -180,10 +180,7 @@ private User saveKakaoUser(KakaoUserResponse response) {
.provider("kakao")
.isAdmin(false)
.userImage(savedUserImage)
.name(response.getKakaoAccount().getName())
.nickname(RandomNickName.generateRandomNickname())
.age(Integer.valueOf(response.getKakaoAccount().getBirthYear()))
.sex(response.getKakaoAccount().getGender())
.email(response.getKakaoAccount().getEmail())
.createdAt(LocalDateTime.now())
.build();
Expand All @@ -198,7 +195,7 @@ private String getKakaoUserImage(KakaoUserResponse response) {
if (Objects.equals(gender, "female")) {
return USER_PROFILE_FEMALE;
}
return "none";
return USER_PROFILE_NONE;
}

private String getAuthLink(String type, String redirectUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import solitour_backend.solitour.information.dto.response.InformationBriefResponse;
import solitour_backend.solitour.user.dto.UpdateAgeAndSex;
import solitour_backend.solitour.user.dto.UpdateNicknameRequest;
import solitour_backend.solitour.user.dto.request.UpdateUserInfoRequest;
import solitour_backend.solitour.user.exception.NicknameAlreadyExistsException;
import solitour_backend.solitour.user.exception.UserNotExistsException;
import solitour_backend.solitour.user.service.UserService;
Expand All @@ -50,6 +51,13 @@ public ResponseEntity<UserInfoResponse> retrieveUserInfo(@AuthenticationPrincipa
return ResponseEntity.ok(response);
}

@PutMapping("/info")
public ResponseEntity<Void> updateUserInfo(@AuthenticationPrincipal Long userId, @RequestBody UpdateUserInfoRequest request) {
userService.updateUserInfo(userId, request);

return ResponseEntity.noContent().build();
}

@PutMapping("/nickname")
public ResponseEntity<String> updateNickname(@AuthenticationPrincipal Long userId,
@RequestBody UpdateNicknameRequest request) {
Expand All @@ -65,19 +73,6 @@ public ResponseEntity<String> updateNickname(@AuthenticationPrincipal Long userI
}
}

@PutMapping("/age-sex")
public ResponseEntity<String> updateAgeAndSex(@AuthenticationPrincipal Long userId,
@RequestBody UpdateAgeAndSex request) {
try {
userService.updateAgeAndSex(userId, request.age(), request.sex());
return ResponseEntity.ok("Age and Sex updated successfully");
} catch (UserNotExistsException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An internal error occurred");
}
}

@PutMapping("/profile")
public ResponseEntity<Void> updateUserProfile(@AuthenticationPrincipal Long userId,
@RequestPart(value = "userProfile", required = false) MultipartFile userProfile) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package solitour_backend.solitour.user.dto.request;

import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Getter
public class UpdateUserInfoRequest {
private String name;
private String age;
private String sex;
}
8 changes: 8 additions & 0 deletions src/main/java/solitour_backend/solitour/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import solitour_backend.solitour.user.dto.request.UpdateUserInfoRequest;
import solitour_backend.solitour.user.user_status.UserStatus;
import solitour_backend.solitour.user.user_status.UserStatusConverter;
import solitour_backend.solitour.user_image.entity.UserImage;
Expand Down Expand Up @@ -103,4 +104,11 @@ public void updateUserImage(String imageUrl) {
public void updateLoginTime() {
this.latestLoginAt = LocalDateTime.now();
}

public void updateUserInfo(UpdateUserInfoRequest request) {
this.name = request.getName();
this.userStatus = UserStatus.ACTIVE;
this.age = Integer.valueOf(request.getAge());
this.sex = request.getSex();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import solitour_backend.solitour.gathering.dto.response.GatheringMypageResponse;
import solitour_backend.solitour.image.s3.S3Uploader;
import solitour_backend.solitour.information.dto.response.InformationBriefResponse;
import solitour_backend.solitour.user.dto.request.UpdateUserInfoRequest;
import solitour_backend.solitour.user.entity.User;
import solitour_backend.solitour.user.exception.NicknameAlreadyExistsException;
import solitour_backend.solitour.user.repository.UserRepository;
Expand All @@ -38,18 +39,12 @@ public UserInfoResponse retrieveUserInfo(Long userId) {
@Transactional
public void updateNickname(Long userId, String nickname) {
if (userRepository.existsByNickname(nickname)) {
throw new NicknameAlreadyExistsException("Nickname already exists");
throw new NicknameAlreadyExistsException("이미 존재하는 닉네임입니다.");
}
User user = userRepository.findByUserId(userId);
user.updateNickname(nickname);
}

@Transactional
public void updateAgeAndSex(Long userId, String age, String sex) {
User user = userRepository.findByUserId(userId);
user.updateAgeAndSex(age, sex);
}

public Page<InformationBriefResponse> retrieveInformationOwner(Pageable pageable, Long userId) {
return userRepository.retrieveInformationOwner(pageable, userId);
}
Expand All @@ -76,4 +71,10 @@ public Page<GatheringMypageResponse> retrieveGatheringBookmark(Pageable pageable
public Page<GatheringApplicantResponse> retrieveGatheringApplicant(Pageable pageable, Long userId) {
return userRepository.retrieveGatheringApplicant(pageable, userId);
}

@Transactional
public void updateUserInfo(Long userId, UpdateUserInfoRequest request) {
User user = userRepository.findByUserId(userId);
user.updateUserInfo(request);
}
}

0 comments on commit 1c71e26

Please sign in to comment.