From af651e1a0d500d1610493b8f3cc16e7919f41541 Mon Sep 17 00:00:00 2001 From: jiinkyung Date: Tue, 20 Feb 2024 00:46:02 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9BFix:=20token=EA=B3=BC=20=ED=95=A8?= =?UTF-8?q?=EA=BB=98=20=EB=A6=AC=EB=8B=A4=EC=9D=B4=EB=A0=89=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/MainPageController.java | 23 ++++++++++++++----- .../domain/mainpage/dto/MainPageDTO.java | 5 ++-- .../mainpage/service/MainPageService.java | 8 ++++--- .../mainpage/service/MainPageServiceImpl.java | 19 +++++++-------- .../oauth/handler/OAuth2SuccessHandler.java | 15 ++++++------ 5 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/trendithon/timetris/domain/mainpage/controller/MainPageController.java b/src/main/java/com/trendithon/timetris/domain/mainpage/controller/MainPageController.java index 1d23d63..49738ec 100644 --- a/src/main/java/com/trendithon/timetris/domain/mainpage/controller/MainPageController.java +++ b/src/main/java/com/trendithon/timetris/domain/mainpage/controller/MainPageController.java @@ -2,6 +2,7 @@ import com.trendithon.timetris.domain.mainpage.dto.MainPageDTO; import com.trendithon.timetris.domain.mainpage.service.MainPageService; +import com.trendithon.timetris.domain.member.dto.MyPageResponse; import com.trendithon.timetris.global.auth.jwt.TokenProvider; import com.trendithon.timetris.global.exception.ApiResponse; import com.trendithon.timetris.global.exception.enums.SuccessStatus; @@ -23,15 +24,25 @@ public class MainPageController { private final TokenProvider tokenProvider; @GetMapping - public ApiResponse getMainPage(HttpServletRequest request) { - String userName = (String) request.getSession().getAttribute("name"); - String imgUrl = (String) request.getSession().getAttribute("picture"); - //String accessToken = (String) request.getSession().getAttribute("token"); + public ApiResponse getMainPage(HttpServletRequest request) { - Long userId = mainPageService.getUserId(userName, imgUrl); + Long userId = tokenProvider.getUserId(request); + + MyPageResponse.getMyPageDTO mainPageDTO = mainPageService.getUserInfo(userId); - MainPageDTO mainPageDTO = mainPageService.getMainPage(userId, request); return ApiResponse.success(SuccessStatus.OK, mainPageDTO); } +// @GetMapping +// public ApiResponse getMainPage(HttpServletRequest request) { +// String userName = (String) request.getSession().getAttribute("name"); +// String imgUrl = (String) request.getSession().getAttribute("picture"); +// //String accessToken = (String) request.getSession().getAttribute("token"); +// +// Long userId = mainPageService.getUserId(userName, imgUrl); +// +// MainPageDTO mainPageDTO = mainPageService.getMainPage(userId, request); +// return ApiResponse.success(SuccessStatus.OK, mainPageDTO); +// } + } diff --git a/src/main/java/com/trendithon/timetris/domain/mainpage/dto/MainPageDTO.java b/src/main/java/com/trendithon/timetris/domain/mainpage/dto/MainPageDTO.java index c35f957..7dd10dd 100644 --- a/src/main/java/com/trendithon/timetris/domain/mainpage/dto/MainPageDTO.java +++ b/src/main/java/com/trendithon/timetris/domain/mainpage/dto/MainPageDTO.java @@ -17,9 +17,8 @@ public class MainPageDTO { List doViewDTOList; List seeViewDTO; String userName; - String accessToken; - public static MainPageDTO from(String accessToken, String username, List planList, List doList, List seeList){ + public static MainPageDTO from(String userName, List planList, List doList, List seeList){ List planViewDTOS = planList.stream() .map(PlanViewDTO::of) .toList(); @@ -30,7 +29,7 @@ public static MainPageDTO from(String accessToken, String username, List p .map(SeeViewDTO::of) .toList(); - return new MainPageDTO(planViewDTOS, doViewDTOS, seeViewDTO1, username, accessToken); + return new MainPageDTO(planViewDTOS, doViewDTOS, seeViewDTO1, userName); } diff --git a/src/main/java/com/trendithon/timetris/domain/mainpage/service/MainPageService.java b/src/main/java/com/trendithon/timetris/domain/mainpage/service/MainPageService.java index f01aaa3..d229256 100644 --- a/src/main/java/com/trendithon/timetris/domain/mainpage/service/MainPageService.java +++ b/src/main/java/com/trendithon/timetris/domain/mainpage/service/MainPageService.java @@ -1,14 +1,16 @@ package com.trendithon.timetris.domain.mainpage.service; import com.trendithon.timetris.domain.mainpage.dto.MainPageDTO; -import jakarta.servlet.http.HttpServletRequest; +import com.trendithon.timetris.domain.member.dto.MyPageResponse; import org.springframework.stereotype.Service; + @Service public interface MainPageService { - MainPageDTO getMainPage(long userId, HttpServletRequest request); + MainPageDTO getMainPage(long userId); + + MyPageResponse.getMyPageDTO getUserInfo(Long userId); - Long getUserId(String username, String imgUrl); void createUserDate(); } diff --git a/src/main/java/com/trendithon/timetris/domain/mainpage/service/MainPageServiceImpl.java b/src/main/java/com/trendithon/timetris/domain/mainpage/service/MainPageServiceImpl.java index 6fb74fe..05fc864 100644 --- a/src/main/java/com/trendithon/timetris/domain/mainpage/service/MainPageServiceImpl.java +++ b/src/main/java/com/trendithon/timetris/domain/mainpage/service/MainPageServiceImpl.java @@ -1,6 +1,7 @@ package com.trendithon.timetris.domain.mainpage.service; import com.trendithon.timetris.domain.member.domain.User; +import com.trendithon.timetris.domain.member.dto.MyPageResponse; import com.trendithon.timetris.domain.member.repository.UserRepository; import com.trendithon.timetris.domain.mainpage.domain.*; import com.trendithon.timetris.domain.mainpage.dto.DateCreateDTO; @@ -9,7 +10,6 @@ import com.trendithon.timetris.domain.mainpage.repository.*; import com.trendithon.timetris.global.exception.CustomException; import com.trendithon.timetris.global.exception.enums.ErrorStatus; -import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -30,13 +30,10 @@ public class MainPageServiceImpl implements MainPageService{ private final UserRepository userRepository; @Override - public MainPageDTO getMainPage(long userId, HttpServletRequest request) { + public MainPageDTO getMainPage(long userId) { LocalDate localDate = LocalDate.now(); Date date = dateRepository.findByDate(localDate); - String userName = (String) request.getSession().getAttribute("name"); - String accessToken = (String) request.getSession().getAttribute("token"); - if (userRepository.findById(userId).isEmpty()){ throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR); } @@ -45,13 +42,17 @@ public MainPageDTO getMainPage(long userId, HttpServletRequest request) { List planList = planRepository.findAllByUserDate(userDate); List doList = doRepository.findAllByUserDate(userDate); List see = seeRepository.findByUserDate(userDate); + String name = userRepository.findById(userId).get().getNickname(); - return MainPageDTO.from(accessToken, userName, planList, doList, see); + return MainPageDTO.from(name, planList, doList, see); } @Override - public Long getUserId(String userName, String imgUrl) { - Optional user = userRepository.findByNameAndProfile(userName, imgUrl); - return user.get().getId(); + public MyPageResponse.getMyPageDTO getUserInfo(Long userId) { + Optional user = userRepository.findById(userId); + return MyPageResponse.getMyPageDTO.builder() + .name(user.get().getName()) + .email(user.get().getEmail()) + .build(); } @Override diff --git a/src/main/java/com/trendithon/timetris/global/auth/oauth/handler/OAuth2SuccessHandler.java b/src/main/java/com/trendithon/timetris/global/auth/oauth/handler/OAuth2SuccessHandler.java index 8f78c8d..ef22d23 100644 --- a/src/main/java/com/trendithon/timetris/global/auth/oauth/handler/OAuth2SuccessHandler.java +++ b/src/main/java/com/trendithon/timetris/global/auth/oauth/handler/OAuth2SuccessHandler.java @@ -32,8 +32,8 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo try { CustomOAuth2User oAuth2User = (CustomOAuth2User) authentication.getPrincipal(); - String userName = oAuth2User.getAttribute("name"); - String imgUrl = oAuth2User.getAttribute("picture"); +// String userName = oAuth2User.getAttribute("name"); +// String imgUrl = oAuth2User.getAttribute("picture"); String accessToken; if (oAuth2User.getRole() == Role.GUEST) { @@ -44,13 +44,14 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo } else { accessToken = loginSuccess(response, oAuth2User); } - request.getSession().setAttribute("token", accessToken); - request.getSession().setAttribute("name", userName); - request.getSession().setAttribute("picture", imgUrl); + String redirectUrl = "http://localhost:3000?accessToken=" + accessToken; - //response.sendRedirect("/main"); - response.sendRedirect(UriComponentsBuilder.fromUriString("http://localhost:3000/").toUriString()); +// request.getSession().setAttribute("token", accessToken); +// request.getSession().setAttribute("name", userName); +// request.getSession().setAttribute("picture", imgUrl); + + response.sendRedirect(redirectUrl); } catch (Exception e) { throw e;