Skip to content

Commit

Permalink
Merge pull request #142 from tukcomCD2024/fix/#141-backend-kakao-login
Browse files Browse the repository at this point in the history
Fix/#141 회원 정보 저장 및 닉네임 업데이트 로직 수정
  • Loading branch information
yeonjy authored Jun 17, 2024
2 parents d82f42b + de9d356 commit 826ced8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@
public class MemberController implements MemberApi{
private final MemberService memberService;

@PostMapping("")
public ResponseEntity<HttpStatus> updateMember(@LoginMemberEmail String email,
@RequestBody MemberUpdateDto memberUpdateDto) {
MemberServiceDto memberServiceDto = memberUpdateDto.toServiceDto(email);

if (memberService.isDuplicatedNickname(memberServiceDto)) {
return ResponseEntity.status(HttpStatus.CONFLICT).build();
}
memberService.update(memberServiceDto);

return ResponseEntity.status(HttpStatus.OK).build();
}

@ResponseStatus(HttpStatus.OK)
@GetMapping("")
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rollthedice.backend.domain.member.service;

import com.rollthedice.backend.domain.member.dto.MemberServiceDto;
import com.rollthedice.backend.domain.member.dto.MemberUpdateDto;
import com.rollthedice.backend.domain.member.dto.response.MemberResponse;
import com.rollthedice.backend.domain.member.entity.Member;
import com.rollthedice.backend.domain.member.exception.MemberNotFoundException;
Expand All @@ -11,9 +12,11 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@RequiredArgsConstructor
@Service
public class MemberService {
Expand All @@ -27,8 +30,9 @@ public boolean isDuplicatedNickname(MemberServiceDto memberServiceDto) {
}

@Transactional
public void update(MemberServiceDto memberServiceDto) {
findByEmail(memberServiceDto.getEmail()).update(memberServiceDto);
public void update(MemberUpdateDto memberUpdateDto) {
Member loginMember = authService.getMember();
loginMember.signUp(memberUpdateDto.getNickname());
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Member toEntity(SocialType socialType, OAuth2UserInfo oauth2UserInfo) {
.email(oauth2UserInfo.getEmail())
.nickname(oauth2UserInfo.getNickname())
.imageUrl(oauth2UserInfo.getImageUrl())
.role(Role.GUEST)
.role(Role.USER)
.status(Status.ACTIVE)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
package com.rollthedice.backend.global.oauth2.controller;

import com.rollthedice.backend.domain.member.dto.MemberUpdateDto;
import com.rollthedice.backend.domain.member.service.MemberService;
import com.rollthedice.backend.global.annotation.LoginMemberEmail;
import com.rollthedice.backend.global.oauth2.dto.LoginRequest;
import com.rollthedice.backend.global.oauth2.service.AuthService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequiredArgsConstructor
public class AuthController {
private final AuthService authService;
private final MemberService memberService;

@PostMapping("/login")
public ResponseEntity<HttpStatus> login(@RequestBody LoginRequest request, HttpServletResponse response) {
authService.authenticateOrRegisterUser(request, response);
return new ResponseEntity<>(HttpStatus.OK);
}

@PostMapping("/oauth2/sign-up")
public ResponseEntity<HttpStatus> updateMember(@LoginMemberEmail String email,
@RequestBody MemberUpdateDto memberUpdateDto) {
memberService.update(memberUpdateDto);

return ResponseEntity.status(HttpStatus.OK).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ private Member registerMember(SocialType socialType, OAuth2UserInfo userInfo) {
Member member = Member.builder()
.socialType(socialType)
.oauthId(userInfo.getId())
.email(UUID.randomUUID() + "@socialUser.com")
.nickname(String.valueOf(UUID.randomUUID()))
.email(userInfo.getEmail())
.nickname(userInfo.getNickname())
.imageUrl(userInfo.getImageUrl())
.role(Role.USER)
.build();
Expand All @@ -51,10 +51,9 @@ private Member registerMember(SocialType socialType, OAuth2UserInfo userInfo) {
}

public Member getMember() {

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
return memberRepository.findByEmail(userDetails.getUsername()).orElseThrow(MemberNotFoundException::new);
String username = authentication.getName();
return memberRepository.findByEmail(username).orElseThrow(MemberNotFoundException::new);
}

}

0 comments on commit 826ced8

Please sign in to comment.