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

Refactor: 회원가입 및 로그인 코드 수정 #172

Merged
merged 1 commit into from
Aug 30, 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
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ public class UserAuthControllerImpl implements AuthController {

@PostMapping("/auth/signup")
public ResponseEntity<ApiResponse<UserAuthResponse>> signUp(
@RequestHeader(HeaderConstants.AUTHORIZATION) final String accessTokenWithBearer,
@RequestHeader(HeaderConstants.AUTHORIZATION) final String tokenWithBearer,
@RequestBody final UserSignUpRequest userSignUpRequest) {
final UserAuthResponse response = userApplicationService.signUp(userSignUpRequest,
accessTokenWithBearer);
tokenWithBearer);
return ResponseEntity.status(HttpStatus.CREATED)
.body(ApiResponse.success(SuccessType.CREATED_SUCCESS, response));
}

@ResponseBody
@PostMapping("/auth/signin")
public ResponseEntity<ApiResponse<UserAuthResponse>> signIn(
@RequestHeader(HeaderConstants.AUTHORIZATION) String accessTokenWithBearer,
@RequestHeader(HeaderConstants.AUTHORIZATION) String tokenWithBearer,
@RequestBody final UserSignInRequest userSignInRequest) {
final UserAuthResponse response = userApplicationService.signIn(userSignInRequest,
accessTokenWithBearer);
tokenWithBearer);
return ResponseEntity.status(HttpStatus.OK)
.body(ApiResponse.success(SuccessType.OK_SUCCESS, response));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
public record UserSignInRequest(
@Schema(description = "플랫폼", example = "apple/kakao")
String platform,
@Schema(description = "애플 로그인", example = "ey123fg.asdafa.asddsa")
String id_token,
String fcmToken
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ public record UserSignUpRequest(
String platform,
String email,
String name,
@Schema(description = "애플 로그인", example = "ey23asdf.asdasdd.asdasd")
String id_token,

String fcmToken
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,15 @@
public class UserMapper {

public static UserDomainInfo toDomainInfo(UserSignUpRequest request,
String authToken) {
if(request.id_token()==null){
return UserDomainInfo.toUserDomainInfo(Platform.fromString(request.platform()),
authToken);
}
return UserDomainInfo.toUserDomainInfo(Platform.fromString(request.platform()),
request.id_token(), authToken);
String tokenWithBearer) {

return UserDomainInfo.toUserDomainInfo(Platform.fromString(request.platform()), tokenWithBearer);
}

public static UserDomainInfo toDomainInfo(UserSignInRequest request,
String authToken) {
if(request.id_token()==null){
return UserDomainInfo.toUserDomainInfo(Platform.fromString(request.platform()),
authToken);
}
return UserDomainInfo.toUserDomainInfo(Platform.fromString(request.platform()),
request.id_token(), authToken);
String tokenWithBearer) {

return UserDomainInfo.toUserDomainInfo(Platform.fromString(request.platform()), tokenWithBearer);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,22 @@ public UserApplicationService(SocialRegisterStrategyFactory strategyFactory,
}

@Override
public UserAuthResponse signUp(UserSignUpRequest request, String authToken) {
public UserAuthResponse signUp(UserSignUpRequest request, String tokenWithBearer) {
Platform platform = Platform.fromString(request.platform());
UserDomainInfo info = UserMapper.toDomainInfo(request,authToken);
UserDomainInfo info = UserMapper.toDomainInfo(request,tokenWithBearer);
SocialRegisterStrategy strategy = strategyFactory.getStrategy(platform);
UserSocialInfo userSocialInfo = strategy.getUserInfo(info);
User newUser = User.createNewUser(userSocialInfo.id(), platform, request.email(), request.name());

User newUser = null;
if (platform == Platform.APPLE)
{
newUser = User.createNewUser(userSocialInfo.id(), platform, request.email(), request.name());
}
else
{
newUser = User.createNewUser(userSocialInfo.id(), platform, userSocialInfo.email(), request.name());
}

User savedUser = userAuthService.registerUser(newUser);
Token token = issueToken(savedUser.getId());

Expand All @@ -57,9 +67,9 @@ public UserAuthResponse signUp(UserSignUpRequest request, String authToken) {
}

@Override
public UserAuthResponse signIn(UserSignInRequest request, String authToken) {
public UserAuthResponse signIn(UserSignInRequest request, String tokenWithBearer) {
Platform platform = Platform.fromString(request.platform());
UserDomainInfo info = UserMapper.toDomainInfo(request,authToken);
UserDomainInfo info = UserMapper.toDomainInfo(request, tokenWithBearer);
SocialRegisterStrategy strategy = strategyFactory.getStrategy(platform);
UserSocialInfo userSocialInfo = strategy.getUserInfo(info);
User foundUser = userAuthService.findUserByPlatformAndPlatformId(platform, userSocialInfo.id());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@

public record UserDomainInfo(
Platform platform,
String idToken,
String authTokenWithBearer
String tokenWithBearer
) {

public static UserDomainInfo toUserDomainInfo(Platform platform, String authTokenWithBearer) {
return new UserDomainInfo(platform,null, authTokenWithBearer);
public static UserDomainInfo toUserDomainInfo(Platform platform, String tokenWithBearer) {
return new UserDomainInfo(platform, tokenWithBearer);
}

public static UserDomainInfo toUserDomainInfo(Platform platform, String idToken, String authTokenWithBearer) {
return new UserDomainInfo(platform, idToken
, authTokenWithBearer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

public record UserSocialInfo(
String id,
Platform platform
Platform platform,
String email
) {
public static UserSocialInfo of(String id, Platform platform) {
return new UserSocialInfo(id, platform);
return new UserSocialInfo(id, platform, "");
}

public static UserSocialInfo of(String id, Platform platform, String email) {
return new UserSocialInfo(id, platform, email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public class AppleAuthStrategy implements SocialRegisterStrategy {
public UserSocialInfo getUserInfo(UserDomainInfo info) {

Platform platform = info.platform();
String platformId = getApplePlatformId(info.idToken());
String idToken = info.tokenWithBearer().split(" ")[1];
String platformId = getApplePlatformId(idToken);
return UserSocialInfo.of(platformId, platform);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.clody.infra.models.user.strategy;

import com.clody.domain.user.Platform;
import com.clody.domain.user.User;
import com.clody.domain.user.dto.UserDomainInfo;
import com.clody.domain.user.repository.UserRepository;
import com.clody.domain.user.strategy.SocialRegisterStrategy;
Expand All @@ -11,7 +10,6 @@
import com.clody.infra.external.feign.kakao.KakaoAuthClient;
import com.clody.infra.external.feign.kakao.KakaoUserInfoClient;
import com.clody.support.dto.type.ErrorType;
import com.clody.support.exception.auth.SignInException;
import com.clody.support.exception.auth.SignUpException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -38,35 +36,15 @@ public UserSocialInfo getUserInfo(UserDomainInfo info) {
Platform platform = info.platform();

//로컬 테스트시 아래의3줄 주석 해제
KakaoTokenResponse token = getKakaoToken(info.authTokenWithBearer());
String accessTokenWithPrefix = KakaoTokenResponse.getTokenWithPrefix(token.access_token());
KakaoUserInfoResponse userInfo = getKakaoUserInfo(accessTokenWithPrefix);
// KakaoTokenResponse token = getKakaoToken(info.tokenWithBearer());
// String accessTokenWithPrefix = KakaoTokenResponse.getTokenWithPrefix(token.access_token());
// KakaoUserInfoResponse userInfo = getKakaoUserInfo(accessTokenWithPrefix);

//로컬 테스트 아래의1줄 주석 하셈
// KakaoUserInfoResponse userInfo = getKakaoUserInfo(info.authTokenWithBearer());
// validateDuplicateUser(userInfo);
return UserSocialInfo.of(userInfo.id(), platform);
}

// public UserSocialInfo signIn(UserDomainSignInInfo info) {
//
// Platform platform = info.platform();
//
// //로컬 테스트시 아래의3줄 주석 해제
//// KakaoTokenResponse token = getKakaoToken(accessTokenWithBearer);
//// String accessTokenWithPrefix = KakaoTokenResponse.getTokenWithPrefix(token.access_token());
//// KakaoUserInfoResponse userInfo = getKakaoUserInfo(accessTokenWithPrefix);
//
// //로컬 테스트시 아래의 1줄 주석 하셈
// KakaoUserInfoResponse userInfo = getKakaoUserInfo(info.authTokenWithBearer());
//
//// return findByPlatformAndPlatformId(platform, userInfo.id());
// }
KakaoUserInfoResponse userInfo = getKakaoUserInfo(info.tokenWithBearer());

private User findByPlatformAndPlatformId(Platform platform, String platformId) {
return userRepository.findByPlatformAndPlatformID(platform, platformId).orElseThrow(
() -> new SignInException(ErrorType.USER_NOT_FOUND)
);
validateDuplicateUser(userInfo);
return UserSocialInfo.of(userInfo.id(), platform, userInfo.kakaoAccount().email());
}

private KakaoTokenResponse getKakaoToken(String authToken) {
Expand All @@ -88,8 +66,4 @@ private void validateDuplicateUser(KakaoUserInfoResponse userInfo) {
throw new SignUpException(ErrorType.DUPLICATED_USER_ERROR);
}
}

private Platform getPlatformFromRequestString(String request) {
return Platform.fromString(request);
}
}
Loading