Skip to content

Commit

Permalink
Merge pull request #20 from Timetris-Trendithon/mainPageError
Browse files Browse the repository at this point in the history
🐛Fix: token과 함께 리다이렉트
  • Loading branch information
jiinkyung authored Feb 19, 2024
2 parents ae688b0 + af651e1 commit a8008a9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,15 +24,25 @@ public class MainPageController {
private final TokenProvider tokenProvider;

@GetMapping
public ApiResponse<MainPageDTO> 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<MyPageResponse.getMyPageDTO> 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<MainPageDTO> 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);
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ public class MainPageDTO {
List<DoViewDTO> doViewDTOList;
List<SeeViewDTO> seeViewDTO;
String userName;
String accessToken;

public static MainPageDTO from(String accessToken, String username, List<Plan> planList, List<Do> doList, List<See> seeList){
public static MainPageDTO from(String userName, List<Plan> planList, List<Do> doList, List<See> seeList){
List<PlanViewDTO> planViewDTOS = planList.stream()
.map(PlanViewDTO::of)
.toList();
Expand All @@ -30,7 +29,7 @@ public static MainPageDTO from(String accessToken, String username, List<Plan> p
.map(SeeViewDTO::of)
.toList();

return new MainPageDTO(planViewDTOS, doViewDTOS, seeViewDTO1, username, accessToken);
return new MainPageDTO(planViewDTOS, doViewDTOS, seeViewDTO1, userName);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -45,13 +42,17 @@ public MainPageDTO getMainPage(long userId, HttpServletRequest request) {
List<Plan> planList = planRepository.findAllByUserDate(userDate);
List<Do> doList = doRepository.findAllByUserDate(userDate);
List<See> 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> user = userRepository.findByNameAndProfile(userName, imgUrl);
return user.get().getId();
public MyPageResponse.getMyPageDTO getUserInfo(Long userId) {
Optional<User> user = userRepository.findById(userId);
return MyPageResponse.getMyPageDTO.builder()
.name(user.get().getName())
.email(user.get().getEmail())
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit a8008a9

Please sign in to comment.