-
Notifications
You must be signed in to change notification settings - Fork 0
구글 OAuth 로그인 코드 리팩토링 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/moongeul/backend/api/member/dto/GoogleInfoResponseDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.moongeul.backend.api.member.dto; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public class GoogleInfoResponseDTO { | ||
|
|
||
| private String id; | ||
| private String email; | ||
| private String name; | ||
| private String picture; | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/moongeul/backend/api/member/dto/GoogleTokenResponseDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.moongeul.backend.api.member.dto; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public class GoogleTokenResponseDTO { | ||
|
|
||
| // JSON의 access_token 필드를 이 변수에 매핑 | ||
| @JsonProperty("access_token") | ||
| private String accessToken; | ||
| } |
3 changes: 3 additions & 0 deletions
3
src/main/java/com/moongeul/backend/api/member/dto/LoginRequestDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| package com.moongeul.backend.api.member.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class LoginRequestDTO { | ||
|
|
||
| @NotBlank(message = "구글 엑세스토큰이 입력되지 않았습니다.") | ||
| private String code; // 인가코드 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/com/moongeul/backend/api/member/service/MemberService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package com.moongeul.backend.api.member.service; | ||
|
|
||
| import com.moongeul.backend.api.member.dto.GoogleInfoResponseDTO; | ||
| import com.moongeul.backend.api.member.dto.GoogleTokenResponseDTO; | ||
| import com.moongeul.backend.api.member.dto.UserInfoDTO; | ||
| import com.moongeul.backend.api.member.entity.Member; | ||
| import com.moongeul.backend.api.member.entity.Role; | ||
| import com.moongeul.backend.api.member.jwt.dto.JwtTokenDTO; | ||
| import com.moongeul.backend.api.member.dto.LoginResponseDTO; | ||
| import com.moongeul.backend.api.member.repository.MemberRepository; | ||
| import com.moongeul.backend.common.config.jwt.JwtTokenProvider; | ||
| import com.moongeul.backend.common.exception.NotFoundException; | ||
| import com.moongeul.backend.common.response.ErrorStatus; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class MemberService { | ||
|
|
||
| private final MemberRepository memberRepository; | ||
| private final JwtTokenProvider jwtTokenProvider; | ||
| private final OAuthService oAuthService; | ||
|
|
||
| // 인가코드 받아 JWT로 교환 및 회원가입/로그인 처리 | ||
| @Transactional | ||
| public LoginResponseDTO loginWithGoogle(String code){ | ||
|
|
||
| // 1. 인가 코드로 Google Access Token 및 사용자 정보 획득 | ||
| GoogleTokenResponseDTO tokenResponse = oAuthService.getGoogleToken(code); | ||
| GoogleInfoResponseDTO userInfo = oAuthService.getGoogleUserInfo(tokenResponse.getAccessToken()); | ||
|
|
||
| // 2. 사용자 정보 추출 | ||
| String socialId = userInfo.getId(); | ||
| String email = userInfo.getEmail(); | ||
| String name = userInfo.getName(); | ||
| String picture = userInfo.getPicture(); | ||
|
|
||
| // 3. DB 처리 (회원가입 또는 로그인) | ||
| Member member = memberRepository.findBySocialId(socialId) | ||
| .map(entity -> entity.update(name, picture)) // 이미 있으면 정보 업데이트 | ||
| .orElseGet(() -> signUp(socialId, email, name, picture)); // 없으면 신규 회원가입 | ||
|
|
||
| // 4. 자체 JWT 토큰 생성 및 반환 | ||
| JwtTokenDTO jwtToken = jwtTokenProvider.generateToken(member); | ||
| member.updateRefreshToken(jwtToken.getRefreshToken()); // 생성된 refreshToken DB 저장 | ||
|
|
||
| return LoginResponseDTO.builder() | ||
| .role(member.getAuthorityKey()) | ||
| .accessToken(jwtToken.getAccessToken()) | ||
| .refreshToken(jwtToken.getRefreshToken()) | ||
| .build(); | ||
| } | ||
|
|
||
| // 신규 회원가입 처리 로직 (DB 저장) | ||
| private Member signUp(String socialId, String email, String name, String picture) { | ||
| Member newUser = Member.builder() | ||
| .email(email) | ||
| .name(name) | ||
| .profileImage(picture) | ||
| .password("OAuth Password") // 임시 패스워드 | ||
| .socialId(socialId) // 예시 사용자명 생성 | ||
| .socialType("google") | ||
| .role(Role.GUEST) // 이후 필요 정보 모두 입력 시 USER 로 승격 | ||
| .build(); | ||
| return memberRepository.save(newUser); | ||
| } | ||
|
|
||
| // 사용자 정보 조회 | ||
| @Transactional(readOnly = true) | ||
| public UserInfoDTO getUserInfo(String email){ | ||
|
|
||
| Member member = getMemberByEmail(email); | ||
|
|
||
| return UserInfoDTO.builder() | ||
| .id(member.getId()) | ||
| .name(member.getName()) | ||
| .profileImage(member.getProfileImage()) | ||
| .nickname(member.getNickname()) | ||
| .build(); | ||
| } | ||
|
|
||
| private Member getMemberByEmail(String email) { | ||
| return memberRepository.findByEmail(email) | ||
| .orElseThrow(() -> new NotFoundException(ErrorStatus.USER_NOTFOUND_EXCEPTION.getMessage())); | ||
| } | ||
| } | ||
190 changes: 0 additions & 190 deletions
190
src/main/java/com/moongeul/backend/api/member/service/MemeberService.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
회원 생성은 여러 DB 쓰기를 하나의 작업으로 묶어 예외 발생 시 전체 롤백 및 데이터 정합성을 보장해야 하므로, 회원가입 메서드에 @transactional을 추가해주세요!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
signUp 메서드는 부모 메서드(loginWithGoogle)가 제공하는 트랜잭션 범위 내에서 실행되고 있기 때문에 추가적인 @transactional을 넣지 않았습니다!
피드백 감사합니다 :)