Skip to content

Commit

Permalink
[MERGE/#166] 액세스 토큰 생성 시 유저 회원가입 여부 판단 로직 리팩토링
Browse files Browse the repository at this point in the history
[REFACTOR/#166] 액세스 토큰 생성 시 유저 회원가입 여부 판단 로직 리팩토링
  • Loading branch information
ckkim817 authored Nov 13, 2024
2 parents 31542d7 + 081d7ee commit 145cd36
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
@JsonInclude(JsonInclude.Include.NON_NULL)
public record LoginSuccessResponse(
String role,
String accessToken
String accessToken,
String nickname
) {
public static LoginSuccessResponse of(
final String role,
final String accessToken
final String accessToken,
final String nickname
) {
return new LoginSuccessResponse(
role,
accessToken
accessToken,
nickname
);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
package org.sopt.seonyakServer.domain.member.dto;

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public record MemberJoinResponse(
Long seniorId,
String role
) {
public static MemberJoinResponse of(
final Long seniorId,
final String role
) {
return new MemberJoinResponse(
seniorId,
role
);
public static MemberJoinResponse of(final String role) {
return new MemberJoinResponse(role);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,55 +93,38 @@ private MemberInfoResponse getMemberInfoResponse(
// Access Token을 생성할 때, 해당 유저의 회원가입 여부를 판단
private LoginSuccessResponse getTokenDto(final MemberInfoResponse memberInfoResponse) {
try {
Member member;

if (isExistingMember(memberInfoResponse.socialType(), memberInfoResponse.socialId())) {
Member member = memberRepository.findBySocialTypeAndSocialIdOrThrow(
member = memberRepository.findBySocialTypeAndSocialIdOrThrow(
memberInfoResponse.socialType(),
memberInfoResponse.socialId()
);

String role = null;

if (member.getSenior() == null) {
if (member.getPhoneNumber() != null) { // 멤버 엔티티에 전화번호가 없으면 온보딩을 완료하지 못한 것으로 간주
role = "JUNIOR";
}
} else {
role = (member.getSenior().getCatchphrase() != null) ? "SENIOR" : "SENIOR_PENDING";
}

return getTokenByMemberId(
role,
member.getId()
);
} else {
Member member = Member.builder()
member = Member.builder()
.socialType(memberInfoResponse.socialType())
.socialId(memberInfoResponse.socialId())
.email(memberInfoResponse.email())
.build();

return getTokenByMemberId(null, memberRepository.save(member).getId());
member = memberRepository.save(member);
}

String role = determineRole(member);
String nickname = determineNickname(member);

return getTokenByMemberId(role, member.getId(), nickname);

} catch (DataIntegrityViolationException e) { // DB 무결성 제약 조건 위반 예외
Member member = memberRepository.findBySocialTypeAndSocialIdOrThrow(
memberInfoResponse.socialType(),
memberInfoResponse.socialId()
);

String role = null;

if (member.getSenior() == null) {
if (member.getPhoneNumber() != null) { // 멤버 엔티티에 전화번호가 없으면 온보딩을 완료하지 못한 것으로 간주
role = "JUNIOR";
}
} else {
role = (member.getSenior().getCatchphrase() != null) ? "SENIOR" : "SENIOR_PENDING";
}
String role = determineRole(member);
String nickname = determineNickname(member);

return getTokenByMemberId(
role,
member.getId()
);
return getTokenByMemberId(role, member.getId(), nickname);
}
}

Expand All @@ -154,11 +137,27 @@ private boolean isExistingMember(

public LoginSuccessResponse getTokenByMemberId(
final String role,
final Long id
final Long id,
final String nickname
) {
MemberAuthentication memberAuthentication = new MemberAuthentication(id, null, null);

return LoginSuccessResponse.of(role, jwtTokenProvider.issueAccessToken(memberAuthentication));
return LoginSuccessResponse.of(role, jwtTokenProvider.issueAccessToken(memberAuthentication), nickname);
}

private String determineRole(Member member) {
if (member.getSenior() == null) {
return member.getPhoneNumber() != null ? "JUNIOR" : null;
} else {
return "SENIOR";
}
}

private String determineNickname(Member member) {
if (member.getSenior() != null && member.getSenior().getCatchphrase() == null) {
return member.getNickname();
}
return null;
}

// 닉네임 유효성 검증
Expand Down Expand Up @@ -191,19 +190,13 @@ public MemberJoinResponse patchMemberJoin(MemberJoinRequest memberJoinRequest) {
memberJoinRequest.departmentList()
);

Long seniorId = null;

if ("SENIOR".equals(memberJoinRequest.role())) {
if ("SENIOR_PENDING".equals(memberJoinRequest.role())) {
member.addSenior(seniorService.createSenior(memberJoinRequest, member));
seniorId = member.getSenior().getId();
} else if (!"JUNIOR".equals(memberJoinRequest.role())) {
throw new CustomException(ErrorType.INVALID_USER_TYPE_ERROR);
}

return MemberJoinResponse.of(
seniorId,
memberJoinRequest.role()
);
return MemberJoinResponse.of(memberJoinRequest.role());
}

@Transactional
Expand Down

0 comments on commit 145cd36

Please sign in to comment.