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

Feat : memberStatus 를 추가로 반환 및 member 객체 null 에 대한 처리 #52

Merged
merged 1 commit into from
Sep 17, 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 @@ -2,6 +2,7 @@

import com.linkode.api_server.common.exception.MemberException;
import com.linkode.api_server.common.response.BaseResponse;
import com.linkode.api_server.domain.Member;
import com.linkode.api_server.dto.member.LoginResponse;
import com.linkode.api_server.service.LoginService;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,11 +28,13 @@ public class LoginController {
@GetMapping("/oauth2/redirect")
public BaseResponse<LoginResponse> githubLogin(@RequestParam String code) {
log.info("[MemberController.githubLogin]");
try {
return new BaseResponse<>(loginService.githubLogin(code));
}catch (NoSuchElementException e){
return new BaseResponse<>(NOT_FOUND_MEMBER,null);
}
// try {
// return new BaseResponse<>(loginService.githubLogin(code));
// }catch (MemberEx e){
// return new BaseResponse<>(NOT_FOUND_MEMBER,null);
// }
return new BaseResponse<>(loginService.githubLogin(code));

}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.linkode.api_server.dto.member;

import com.linkode.api_server.domain.Member;
import jakarta.annotation.Nullable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -13,6 +14,7 @@ public class LoginResponse {
/**
* 깃허브 소셜로그인
*/
private boolean memberStatus;
private Long memberId;
private String githubId;
private String accessToken;
Expand Down Expand Up @@ -45,13 +47,14 @@ public static class Studyroom {
}

// 정적 팩토리 메서드
public static LoginResponse of(Member member, String accessToken, String refreshToken, List<Studyroom> studyroomList) {
public static LoginResponse of(boolean memberStatus, @Nullable Member member, String githubId,String accessToken, String refreshToken, List<Studyroom> studyroomList) {
return LoginResponse.builder()
.memberId(member.getMemberId())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

복잡한 요구사항인데 정팩매를 잘 적용하신거같아 좋아보여요!

.githubId(member.getGithubId())
.memberStatus(memberStatus)
.memberId(member != null ? member.getMemberId() : null)
.githubId(githubId)
.accessToken(accessToken)
.refreshToken(refreshToken)
.profile(Profile.from(member))
.profile(member != null ? Profile.from(member) : null)
.studyroomList(studyroomList)
.build();
}
Expand Down
61 changes: 38 additions & 23 deletions src/main/java/com/linkode/api_server/service/LoginService.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,45 @@ public class LoginService {
public LoginResponse githubLogin(String code){
log.info("[LoginService.githubLogin]");
String githubId = getUserInfo(getAccessToken(code)); //깃허브 서버와 통신해서 유저 정보 받아오기
String jwtAccessToken = null;
String jwtRefreshToken = null;
List<LoginResponse.Studyroom> studyroom = null;
boolean memberStatus = false;

try {
Member member = memberRepository.findByGithubIdAndStatus(githubId, BaseStatus.ACTIVE)
.orElseThrow(()-> new MemberException(NOT_FOUND_MEMBER));
log.info("[깃허브 아이디로 member 찾기]");

if(member!=null){
memberStatus = true;
jwtAccessToken = jwtProvider.createAccessToken(member);
log.info("[엑세스토큰 발급~]");
jwtRefreshToken = jwtProvider.createRefreshToken(member);
log.info("[리프레시토큰 발급~]");
// 레디스 저장
tokenService.storeToken(jwtRefreshToken, githubId);
LoginResponse.Profile.from(member);
studyroom = memberstudyroomRepository.findByMemberIdAndStatus(member.getMemberId(), BaseStatus.ACTIVE)
.orElseThrow(()->new MemberStudyroomException(NOT_FOUND_MEMBER_STUDYROOM))
.stream()
.map(ms -> new LoginResponse.Studyroom(ms.getStudyroom().getStudyroomId(), ms.getStudyroom().getStudyroomProfile()))
.collect(Collectors.toList());
log.info("[studyroom stream 으로 찾아서 반환하기]");

}
return LoginResponse.of(
memberStatus,
member,
githubId,
jwtAccessToken,
jwtRefreshToken,
studyroom
);
}catch (MemberException e){
return LoginResponse.of(memberStatus, null, githubId, null,null,null);
}

Optional<Member> member = memberRepository.findByGithubIdAndStatus(githubId, BaseStatus.ACTIVE);
log.info("[깃허브 아이디로 member 찾기]");

String jwtAccessToken = jwtProvider.createAccessToken(member.get());
log.info("[엑세스토큰 발급~]");
String jwtRefreshToken = jwtProvider.createRefreshToken(member.get());
log.info("[리프레시토큰 발급~]");
// 레디스 저장
tokenService.storeToken(jwtRefreshToken, githubId);
LoginResponse.Profile.from(member.get());
List<LoginResponse.Studyroom> studyroom = memberstudyroomRepository.findByMemberIdAndStatus(member.get().getMemberId(), BaseStatus.ACTIVE)
.orElseThrow(()->new MemberStudyroomException(NOT_FOUND_MEMBER_STUDYROOM))
.stream()
.map(ms -> new LoginResponse.Studyroom(ms.getStudyroom().getStudyroomId(), ms.getStudyroom().getStudyroomProfile()))
.collect(Collectors.toList());
log.info("[studyroom stream 으로 찾아서 반환하기]");

return LoginResponse.of(
member.get(),
jwtAccessToken,
jwtRefreshToken,
studyroom
);
}

/**
Expand Down
Loading