diff --git a/src/main/java/balancetalk/global/oauth2/CustomSuccessHandler.java b/src/main/java/balancetalk/global/oauth2/CustomSuccessHandler.java index d2b7d5a28..ab7476801 100644 --- a/src/main/java/balancetalk/global/oauth2/CustomSuccessHandler.java +++ b/src/main/java/balancetalk/global/oauth2/CustomSuccessHandler.java @@ -11,7 +11,6 @@ import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.Authentication; import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; import org.springframework.stereotype.Component; @@ -41,6 +40,8 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo response.addCookie(JwtTokenProvider.createCookie(refreshToken)); response.addCookie(JwtTokenProvider.createAccessCookie(accessToken)); + + String redirectUrl = customUserDetails.getRedirectUrl(); response.sendRedirect(redirectUrl); } } diff --git a/src/main/java/balancetalk/global/oauth2/dto/CustomOAuth2User.java b/src/main/java/balancetalk/global/oauth2/dto/CustomOAuth2User.java index f7d2ad466..3843a950e 100644 --- a/src/main/java/balancetalk/global/oauth2/dto/CustomOAuth2User.java +++ b/src/main/java/balancetalk/global/oauth2/dto/CustomOAuth2User.java @@ -11,12 +11,17 @@ public class CustomOAuth2User implements OAuth2User { private final Oauth2Dto oauth2Dto; + private final String redirectUrl; @Override public Map getAttributes() { return Map.of(); } + public String getRedirectUrl() { + return redirectUrl; + } + @Override public Collection getAuthorities() { Collection collection = new ArrayList<>(); diff --git a/src/main/java/balancetalk/global/oauth2/dto/Oauth2Dto.java b/src/main/java/balancetalk/global/oauth2/dto/Oauth2Dto.java index 60688ae68..f886e4d37 100644 --- a/src/main/java/balancetalk/global/oauth2/dto/Oauth2Dto.java +++ b/src/main/java/balancetalk/global/oauth2/dto/Oauth2Dto.java @@ -1,7 +1,13 @@ package balancetalk.global.oauth2.dto; + + +import static balancetalk.member.domain.Role.USER; +import static balancetalk.member.domain.SignupType.SOCIAL; + import balancetalk.member.domain.Member; import balancetalk.member.domain.Role; +import balancetalk.member.domain.SignupType; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -14,13 +20,15 @@ public class Oauth2Dto { private String name; private String email; private Role role; + private SignupType signupType; private String password; public Member toEntity() { return Member.builder() .nickname(name) .email(email) - .role(Role.USER) + .role(USER) + .signupType(SOCIAL) .password(password) .build(); } diff --git a/src/main/java/balancetalk/global/oauth2/service/CustomOAuth2UserService.java b/src/main/java/balancetalk/global/oauth2/service/CustomOAuth2UserService.java index af4bad147..d4b48eeae 100644 --- a/src/main/java/balancetalk/global/oauth2/service/CustomOAuth2UserService.java +++ b/src/main/java/balancetalk/global/oauth2/service/CustomOAuth2UserService.java @@ -1,6 +1,9 @@ package balancetalk.global.oauth2.service; -import static balancetalk.global.config.SecurityConfig.*; +import static balancetalk.global.config.SecurityConfig.passwordEncoder; +import static balancetalk.member.domain.Role.USER; +import static balancetalk.member.domain.SignupType.STANDARD; + import balancetalk.global.oauth2.dto.CustomOAuth2User; import balancetalk.global.oauth2.dto.GoogleResponse; import balancetalk.global.oauth2.dto.KakaoResponse; @@ -9,10 +12,8 @@ import balancetalk.global.oauth2.dto.Oauth2Response; import balancetalk.member.domain.Member; import balancetalk.member.domain.MemberRepository; -import balancetalk.member.domain.Role; import java.util.Optional; import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; @@ -20,7 +21,6 @@ import org.springframework.security.oauth2.core.user.OAuth2User; import org.springframework.stereotype.Service; -@Slf4j @Service @RequiredArgsConstructor public class CustomOAuth2UserService extends DefaultOAuth2UserService { @@ -28,6 +28,12 @@ public class CustomOAuth2UserService extends DefaultOAuth2UserService { @Value("${spring.security.security.oauth2-password}") private String oauth2Password; + @Value("${urls.firstRegister}") + private String firstRegisterUrl; + + @Value("${urls.alreadyRegistered}") + private String alreadyRegisteredUrl; + private final MemberRepository memberRepository; @Override @@ -44,21 +50,21 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic }; String email = getEmail(oauth2Response); - String provider = getProvider(oauth2Response); - Member findMember = memberRepository.findByEmail(provider + "_" + email).orElse(null); + Member findMember = memberRepository.findByEmail(email).orElse(null); if (findMember == null) { String encodedPassword = passwordEncoder().encode(oauth2Password); Oauth2Dto oauth2Dto = Oauth2Dto.builder() .name(hideNickname(email)) - .email(provider + "_" + email) - .role(Role.USER) + .email(email) + .role(USER) + .signupType(STANDARD) .password(encodedPassword) .build(); Member newMember = oauth2Dto.toEntity(); memberRepository.save(newMember); - return new CustomOAuth2User(oauth2Dto); + return new CustomOAuth2User(oauth2Dto, firstRegisterUrl); } else { // 회원이 존재할 떄 @@ -67,7 +73,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic .email(findMember.getEmail()) .role(findMember.getRole()) .build(); - return new CustomOAuth2User(oauth2Dto); + return new CustomOAuth2User(oauth2Dto, alreadyRegisteredUrl); } } @@ -87,10 +93,4 @@ private String getEmail(Oauth2Response oauth2Response) { .map(Oauth2Response::getEmail) .orElse("null"); } - - private String getProvider(Oauth2Response oauth2Response) { - return Optional.ofNullable(oauth2Response) - .map(Oauth2Response::getProvider) - .orElse("null"); - } } diff --git a/src/main/java/balancetalk/member/domain/Member.java b/src/main/java/balancetalk/member/domain/Member.java index e1932919d..7546d7e86 100644 --- a/src/main/java/balancetalk/member/domain/Member.java +++ b/src/main/java/balancetalk/member/domain/Member.java @@ -63,6 +63,9 @@ public class Member extends BaseTimeEntity { @Enumerated(value = EnumType.STRING) private Role role; + @Enumerated(value = EnumType.STRING) + private SignupType signupType; + private Long profileImgId; @OneToMany(mappedBy = "member") diff --git a/src/main/java/balancetalk/member/domain/SignupType.java b/src/main/java/balancetalk/member/domain/SignupType.java new file mode 100644 index 000000000..adb1b1e66 --- /dev/null +++ b/src/main/java/balancetalk/member/domain/SignupType.java @@ -0,0 +1,5 @@ +package balancetalk.member.domain; + +public enum SignupType { + SOCIAL, STANDARD +} diff --git a/src/main/java/balancetalk/member/dto/MemberDto.java b/src/main/java/balancetalk/member/dto/MemberDto.java index 14589d854..1f8f6c075 100644 --- a/src/main/java/balancetalk/member/dto/MemberDto.java +++ b/src/main/java/balancetalk/member/dto/MemberDto.java @@ -1,7 +1,11 @@ package balancetalk.member.dto; + +import static balancetalk.member.domain.SignupType.STANDARD; + import balancetalk.member.domain.Member; import balancetalk.member.domain.Role; +import balancetalk.member.domain.SignupType; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.Email; import jakarta.validation.constraints.NotBlank; @@ -54,12 +58,16 @@ public static class JoinRequest { @Schema(description = "회원 권한", example = "USER") private Role role; + @Schema(description = "회원 가입 경로", example = "STANDARD") + private SignupType signupType; + public Member toEntity() { return Member.builder() .nickname(nickname) .email(email) .password(password) .role(role) + .signupType(STANDARD) .profileImgId(profileImgId) .build(); } @@ -116,6 +124,9 @@ public static class MemberResponse { @Schema(description = "저장한 게시글 수", example = "21") private int bookmarkedPostsCount; + @Schema(description = "회원 가입 경로", example = "STANDARD") + private SignupType signupType; + public static MemberResponse fromEntity(Member member, String profileImgUrl) { return MemberResponse.builder() .id(member.getId()) @@ -125,6 +136,7 @@ public static MemberResponse fromEntity(Member member, String profileImgUrl) { .createdAt(member.getCreatedAt()) .postsCount(member.getPostsCount()) .bookmarkedPostsCount(member.getBookmarkedPostsCount()) + .signupType(member.getSignupType()) .build(); } }