From 751453ee159e9ab57d82d83a10577be3c47829fe Mon Sep 17 00:00:00 2001 From: jungeun Date: Tue, 17 Sep 2024 21:47:29 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20memberStatus=20=EB=A5=BC=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=EB=A1=9C=20=EB=B0=98=ED=99=98=20=EB=B0=8F=20member=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=20null=20=EC=97=90=20=EB=8C=80=ED=95=9C=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/LoginController.java | 13 ++-- .../api_server/dto/member/LoginResponse.java | 11 ++-- .../api_server/service/LoginService.java | 61 ++++++++++++------- 3 files changed, 53 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/linkode/api_server/controller/LoginController.java b/src/main/java/com/linkode/api_server/controller/LoginController.java index 49b7656..276431c 100644 --- a/src/main/java/com/linkode/api_server/controller/LoginController.java +++ b/src/main/java/com/linkode/api_server/controller/LoginController.java @@ -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; @@ -27,11 +28,13 @@ public class LoginController { @GetMapping("/oauth2/redirect") public BaseResponse 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)); + } } \ No newline at end of file diff --git a/src/main/java/com/linkode/api_server/dto/member/LoginResponse.java b/src/main/java/com/linkode/api_server/dto/member/LoginResponse.java index ffbd46d..7894511 100644 --- a/src/main/java/com/linkode/api_server/dto/member/LoginResponse.java +++ b/src/main/java/com/linkode/api_server/dto/member/LoginResponse.java @@ -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; @@ -13,6 +14,7 @@ public class LoginResponse { /** * 깃허브 소셜로그인 */ + private boolean memberStatus; private Long memberId; private String githubId; private String accessToken; @@ -45,13 +47,14 @@ public static class Studyroom { } // 정적 팩토리 메서드 - public static LoginResponse of(Member member, String accessToken, String refreshToken, List studyroomList) { + public static LoginResponse of(boolean memberStatus, @Nullable Member member, String githubId,String accessToken, String refreshToken, List studyroomList) { return LoginResponse.builder() - .memberId(member.getMemberId()) - .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(); } diff --git a/src/main/java/com/linkode/api_server/service/LoginService.java b/src/main/java/com/linkode/api_server/service/LoginService.java index 39ccfc7..a765da3 100644 --- a/src/main/java/com/linkode/api_server/service/LoginService.java +++ b/src/main/java/com/linkode/api_server/service/LoginService.java @@ -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 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 = 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 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 - ); } /**