Skip to content
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

[HEENDY-92-login-expand] 로그인 Oauth 타입 확장용 전략 패턴 적용 #29

Merged
merged 3 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/hyundai/app/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum ErrorCode {
UNAUTHORIZED_ACCESS(UNAUTHORIZED, "인증되지 않은 사용자입니다."),
FORBIDDEN_ACCESS(FORBIDDEN, "인가되지 않은 접근입니다."),
MEMBER_NOT_EXIST(NOT_FOUND, "해당하는 회원 oauth id가 존재하지 않습니다."),
OAUTH_INVALID(NOT_FOUND, "해당 oauth는 지원하지 않습니다."),

// 이벤트
EVENT_NOT_EXIST(NOT_FOUND, "해당하는 이벤트가 존재하지 않습니다."),
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/hyundai/app/member/oauth/LoginService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.hyundai.app.member.oauth;

import com.hyundai.app.member.dto.LoginReqDto;
import com.hyundai.app.member.oauth.kakao.KakaoOauthLoginStrategy;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
* @author 황수영
* @since 2024/03/04
* LoginService
*/
@Log4j
@Component
@RequiredArgsConstructor
public class LoginService {

private final List<LoginStrategy> loginStrategies;
private final KakaoOauthLoginStrategy kakaoOauthLoginStrategy;

public LoginStrategy getOAuthLoginClient(LoginReqDto loginRequestDto) {

return this.loginStrategies.stream()
.filter(s -> s.oAuthType().toString().equals(loginRequestDto.getOauthType()))
.findFirst()
.orElse(kakaoOauthLoginStrategy);
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/hyundai/app/member/oauth/LoginStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.hyundai.app.member.oauth;

import com.hyundai.app.member.dto.LoginReqDto;
import com.hyundai.app.member.enumType.OauthType;

/**
* @author 황수영
* @since 2024/03/04
* OAuth 로그인 인터페이스
*/
public interface LoginStrategy {
OauthType oAuthType();
String getEmail(LoginReqDto loginRequestDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.hyundai.app.member.oauth.google;

import com.hyundai.app.member.dto.LoginReqDto;
import com.hyundai.app.member.enumType.OauthType;
import com.hyundai.app.member.oauth.LoginStrategy;

/**
* @author 황수영
* @since 2024/03/04
* 구글 로그인 이후 추가
*/
public class GoogleOauthLoginStrategy implements LoginStrategy {
@Override
public OauthType oAuthType() {
return OauthType.GOOGLE;
}

@Override
public String getEmail(LoginReqDto loginRequestDto) {
return "google@gmail.com";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.hyundai.app.exception.ErrorCode;
import com.hyundai.app.member.dto.LoginReqDto;
import com.hyundai.app.member.enumType.Header;
import com.hyundai.app.member.enumType.OauthType;
import com.hyundai.app.member.oauth.LoginStrategy;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -24,17 +26,23 @@
@Log4j
@Service
@RequiredArgsConstructor
public class KakaoOauthClient {
public class KakaoOauthLoginStrategy implements LoginStrategy {

@Value("${oauth.kakao.request-uri}")
private String reqUri;
private final RestTemplate restTemplate = new RestTemplate();

@Override
public OauthType oAuthType() {
return OauthType.KAKAO;
}

/**
* @author 황수영
* @since 2024/02/12
* OAuth로부터 사용자 정보 이메일 가져오기
*/
@Override
public String getEmail(LoginReqDto loginRequestDto) {
KakaoResDto kakaoResDto = getMemberInfoByLoginToken(loginRequestDto.getLoginToken());
return kakaoResDto.getEmail();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import com.hyundai.app.member.enumType.OauthType;
import com.hyundai.app.member.enumType.Role;
import com.hyundai.app.member.mapper.MemberMapper;
import com.hyundai.app.member.oauth.kakao.KakaoOauthClient;
import com.hyundai.app.member.oauth.LoginService;
import com.hyundai.app.member.oauth.LoginStrategy;
import com.hyundai.app.security.jwt.JwtTokenGenerator;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j;
Expand All @@ -35,7 +36,7 @@ public class MemberServiceImpl implements MemberService {
@Value("${jwt.access-validity}")
private long accessValidity;
private final MemberMapper memberMapper;
private final KakaoOauthClient oAuthClient;
private final LoginService loginService;
private final JwtTokenGenerator authTokenGenerator;
private final AwsS3Config awsS3Config;
private final MemberQrService memberQrService;
Expand All @@ -46,8 +47,9 @@ public class MemberServiceImpl implements MemberService {
* 회원가입/로그인 기능
*/
public LoginResDto login(LoginReqDto loginReqDto) {
String email = oAuthClient.getEmail(loginReqDto);
OauthType oauthType = OauthType.valueOf(loginReqDto.getOauthType().toUpperCase());
LoginStrategy loginStrategy = loginService.getOAuthLoginClient(loginReqDto);
String email = loginStrategy.getEmail(loginReqDto);
OauthType oauthType = loginStrategy.oAuthType();
String oauthId = oauthType.createOauthIdWithEmail(email);
log.debug("로그인 OauthId : " + oauthId);

Expand Down
Loading