Skip to content

Commit

Permalink
Merge pull request #721 from GraudationProject2023/YongHyeon
Browse files Browse the repository at this point in the history
Yong hyeon
  • Loading branch information
dydgus1052 authored Sep 24, 2023
2 parents 5a20bd9 + a2f4604 commit 7293012
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,25 @@ public ResponseEntity<String> exitMember(HttpServletRequest request, @RequestPar
}

@RequestMapping("members/verify/pw") // get post 둘다 가능
public ResponseEntity<String> verifyPw(HttpServletRequest request, @RequestBody ChangeMemberInfo memberInfo) {
public ResponseEntity<String> verifyPw(@RequestBody ChangeMemberInfo memberInfo) {

String email = (String) request.getSession().getAttribute("loginMember");
String pw = memberInfo.getPw();
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Member loginMember = (Member) authentication.getPrincipal();
Member member = memberService.findByEmail(loginMember.getEmail()).get();

if (memberService.findPw(email, pw)) // 비번 일치
if (memberService.findPw(member.getEmail(), memberInfo.getPw())) // 비번 일치
return ResponseEntity.ok().body("{\"result\": true}");
return ResponseEntity.ok().body("{\"result\": false}");
}

@PostMapping("members/change/pw")
public void changePw(HttpServletRequest request, @RequestBody ChangeMemberInfo memberInfo) {
public void changePw(@RequestBody ChangeMemberInfo memberInfo) {

String email = (String) request.getSession().getAttribute("loginMember");
Member member = memberService.findByEmail(email).get();
// String email = (String) request.getSession().getAttribute("loginMember");
// Member member = memberService.findByEmail(email).get();
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Member loginMember = (Member) authentication.getPrincipal();
Member member = memberService.findByEmail(loginMember.getEmail()).get();

memberService.changePw(member, memberInfo.getPw());
}
Expand All @@ -160,88 +164,4 @@ public void changeTypes(HttpServletRequest request, @RequestBody ChangeMemberInf
memberPreferenceService.deleteTypes(member);
memberPreferenceService.setTypes(member, memberInfo.getTypes());
}


// @PostMapping("/members/join")
// // @PostMapping은 @RequestMapping(value = "/members", method= {RequestMethod.POST}) 와 동일
// public void createMember(@RequestBody MemberJoin memberJoin) {
// Member joinMember = Member.builder()
// .name(memberJoin.getName())
// .pw(memberJoin.getPw())
// .email(memberJoin.getEmail())
// .gender(memberJoin.getGender())
// .types(new ArrayList<>())
// .build();
//
// memberService.join(joinMember);
// memberPreferenceService.setTypes(joinMember, memberJoin.getTypes());
//
// /*
// 프론트에서 데이터를 넘길 때에
// name, pw, email, gender, phoneNumber 라는 필드 명은 동일하게 넘어와야함.
// null이 들어와도 DB에 알아서 null로 대입됨
// */
// }

// @GetMapping("/members/trip")
// public MemberTripTotalDTO searchTrip(HttpServletRequest request,
// @RequestParam(required = false, name = "page", defaultValue = "0") int pageNum,
// // @RequestParam은 Json데이터 처리 x
// @RequestParam(required = false, name = "size", defaultValue = "2") int pageSize) {
// HttpSession session = request.getSession(false);
// String email = (String) session.getAttribute("loginMember");
// if (pageNum > 0)
// pageNum--;
//
// Pageable pageable = PageRequest.of(pageNum, pageSize);
// Page<Trip> trippage = memberService.findTrip(email, pageable);
// List<Trip> trips = trippage.getContent();
//
// Long total = trippage.getTotalElements();
// List<MemberTripTitleDTO> result = trips.stream()
// .map(trip -> new MemberTripTitleDTO(trip))
// .collect(Collectors.toList());
//
// return new MemberTripTotalDTO(result, total);
// }

// @GetMapping("/members/trip/result")
// public MemberTripContentDTO searchTrip(@RequestParam("id") String id) {
//
// Trip trip = tripService.findByUUID(id).get();
// return new MemberTripContentDTO(trip);
//
// }

@PostMapping("/members/login")
public ResponseEntity<String> login(@RequestBody MemberLogin loginMember, HttpServletRequest request) {

Member member = loginService.Login(loginMember.getEmail(), loginMember.getPw());

if (member == null) // 이메일이 존재하지 않거나, 비밀번호가 일치하지 않음
return ResponseEntity.ok().body("{\"result\": false}");

// 로그인 성공 처리
/*
서버 -> 클라
1. 서버에서 세션을 만든다. (Key : sessionId, Value : 회원 객체)
2. 생성된 세션ID(랜덤 값)를 쿠키로 전달 한다.
<클라이언트 별로 세션을 만들기 때문에 클라이언트가 실행될 때마다 모두 다른 세션을 만든다.>
3. 로그 아웃시에는 reqeust.getSession(false)를 실행하여 세션을 새로 만드는 일이 없도록 방지한다.
클라 -> 서버
1. 클라이언트는 항상 쿠키를 전달한다.
2. 세션 저장소에서 쿠키를 조회해서 로그인시 보관한 세션 정보를 사용한다.
*/

// 없으면 새로 생성, 있다면 기존에 있는 것 반환
HttpSession session = request.getSession(true);

// 세션에 로그인한 회원 정보 보관
session.setAttribute("loginMember", member.getEmail());

String responseBody = "{\"result\": true, \"name\": \"" + member.getName() + "\"}";
return ResponseEntity.ok().body(responseBody);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MemberTrip {

@QueryProjection
public MemberTrip(Long id, String UUID, String title, String startingDate, String comingDate, String imageUUID,
int recruitNum, int currentNum) {
int currentNum, int recruitNum) {
this.id = id;
this.UUID = UUID;
this.title = title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ public void exit(Member member) {
// 비밀 번호 변경
@Transactional
public void changePw(Member member, String pw) {
member.changePw(pw);
member.changePw(passwordEncoder.encode(CharBuffer.wrap(pw)));
}

// 비밀 번호 찾기
public boolean findPw(String email, String pw) {
Member member = memberRepository.findByEmail(email).get();
return member.getPw().equals(pw);
return member.getPw().equals(passwordEncoder.encode(CharBuffer.wrap(pw)));
}

public List<MemberInfo> memberList(Long id) {
Expand Down

0 comments on commit 7293012

Please sign in to comment.