Skip to content

Commit

Permalink
[feat] 유저 회원가입시 유저 정보 가져오기 구현 (#70)
Browse files Browse the repository at this point in the history
* [feat] 회원가입시 유저 정보 가져오기 기능 구현 #68

* [chore] spotless 적용 #68

* [fix] email 가져오기 로직 변경 #68

* [refac] transaction 범위 수정 #68
  • Loading branch information
wjdtkdgns authored Jul 28, 2023
1 parent 9b1b87c commit f256d45
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public OauthSignInResponse oauthUserIdTokenLogin(
public OauthRegisterResponse oauthUserRegister(
@PathVariable("provider") OauthProvider provider,
@RequestParam("idToken") String idToken,
@RequestParam("oauthAccessToken") String accessToken,
@RequestBody RegisterRequest registerRequest) {
return oauthRegisterUseCase.execute(provider, idToken, registerRequest);
return oauthRegisterUseCase.execute(provider, idToken, accessToken, registerRequest);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package allchive.server.api.auth.model.dto;


import allchive.server.domain.domains.user.domain.enums.OauthProvider;
import allchive.server.infrastructure.oauth.kakao.dto.KakaoInformationResponse;
import lombok.Builder;
import lombok.Getter;

@Getter
public class OauthUserInfoDto {
private String email;
private String name;
private OauthProvider oauthProvider;

@Builder
private OauthUserInfoDto(String email, String name, OauthProvider oauthProvider) {
this.email = email;
this.name = name;
this.oauthProvider = oauthProvider;
}

public static OauthUserInfoDto fromKakao(KakaoInformationResponse response) {
return OauthUserInfoDto.builder()
.email(response.getEmail())
.name(response.getNickName())
.oauthProvider(OauthProvider.KAKAO)
.build();
}
}
;
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ public class RegisterRequest {

@ArraySchema(schema = @Schema(description = "관심 주제", defaultValue = "FOOD"))
private List<@ValidEnum(target = Category.class) Category> categories;

@Schema(defaultValue = "이름", description = "이름, 애플 회원가입용")
private String name;

@Schema(defaultValue = "이메일", description = "이메일, 애플 회원가입용")
private String email;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import allchive.server.core.annotation.UseCase;
import allchive.server.domain.domains.user.adaptor.RefreshTokenAdaptor;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

@UseCase
@RequiredArgsConstructor
public class LogOutUserUseCase {
private final RefreshTokenAdaptor refreshTokenAdaptor;

@Transactional
public void execute() {
Long userId = SecurityUtil.getCurrentUserId();
refreshTokenAdaptor.deleteTokenByUserId(userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import allchive.server.domain.domains.user.domain.enums.OauthProvider;
import allchive.server.domain.domains.user.service.UserDomainService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

@UseCase
@RequiredArgsConstructor
Expand All @@ -19,16 +20,19 @@ public class OauthLoginUseCase {
private final UserDomainService userDomainService;
private final TokenGenerateHelper tokenGenerateHelper;

@Transactional
public OauthSignInResponse loginWithCode(OauthProvider provider, String code, String referer) {
final OauthTokenResponse oauthTokenResponse =
oauthHelper.getCredential(provider, code, referer);
return processLoginWithIdToken(provider, oauthTokenResponse.getIdToken());
}

@Transactional
public OauthSignInResponse loginWithIdToken(OauthProvider provider, String idToken) {
return processLoginWithIdToken(provider, idToken);
}

@Transactional
public OauthSignInResponse devLogin(OauthProvider provider, String code) {
final OauthTokenResponse oauthTokenResponse = oauthHelper.getCredentialDev(provider, code);
return processLoginWithIdToken(provider, oauthTokenResponse.getIdToken());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package allchive.server.api.auth.service;


import allchive.server.api.auth.model.dto.OauthUserInfoDto;
import allchive.server.api.auth.model.dto.request.RegisterRequest;
import allchive.server.api.auth.model.dto.response.OauthRegisterResponse;
import allchive.server.api.auth.service.helper.OauthHelper;
Expand All @@ -12,6 +13,7 @@
import allchive.server.domain.domains.user.domain.enums.OauthProvider;
import allchive.server.domain.domains.user.service.UserDomainService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

@UseCase
@RequiredArgsConstructor
Expand All @@ -20,15 +22,49 @@ public class OauthRegisterUseCase {
private final UserDomainService userDomainService;
private final TokenGenerateHelper tokenGenerateHelper;

@Transactional
public OauthRegisterResponse execute(
OauthProvider provider, String idToken, RegisterRequest registerRequest) {
OauthProvider provider,
String idToken,
String oauthAccessToken,
RegisterRequest request) {
final OauthInfo oauthInfo = oauthHelper.getOauthInfo(provider, idToken);
final User user =
userDomainService.registerUser(
registerRequest.getNickname(),
UrlUtil.convertUrlToKey(registerRequest.getProfileImgUrl()),
registerRequest.getCategories(),
oauthInfo);
final OauthUserInfoDto oauthUserInfoDto = getUserInfo(provider, oauthAccessToken);
final User user = registerUser(provider, oauthInfo, oauthUserInfoDto, request);
return OauthRegisterResponse.from(tokenGenerateHelper.execute(user));
}

private User registerUser(
OauthProvider provider,
OauthInfo oauthInfo,
OauthUserInfoDto oauthUserInfoDto,
RegisterRequest request) {
switch (provider) {
case APPLE:
return userDomainService.registerUser(
request.getName(),
request.getEmail(),
request.getNickname(),
UrlUtil.convertUrlToKey(request.getProfileImgUrl()),
request.getCategories(),
oauthInfo);
default:
return userDomainService.registerUser(
oauthUserInfoDto.getName(),
oauthUserInfoDto.getEmail(),
request.getNickname(),
UrlUtil.convertUrlToKey(request.getProfileImgUrl()),
request.getCategories(),
oauthInfo);
}
}

private OauthUserInfoDto getUserInfo(OauthProvider provider, String oauthAccessToken) {
switch (provider) {
case APPLE:
return null;
default:
return oauthHelper.getUserInfo(provider, oauthAccessToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import allchive.server.domain.domains.user.domain.RefreshTokenEntity;
import allchive.server.domain.domains.user.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

@UseCase
@RequiredArgsConstructor
Expand All @@ -19,6 +20,7 @@ public class TokenRefreshUseCase {
private final UserAdaptor userAdaptor;
private final TokenGenerateHelper tokenGenerateHelper;

@Transactional
public OauthRegisterResponse execute(String refreshToken) {
RefreshTokenEntity oldToken = refreshTokenAdaptor.findTokenByRefreshToken(refreshToken);
Long userId = jwtTokenProvider.parseRefreshToken(oldToken.getRefreshToken());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package allchive.server.api.auth.service.helper;

import static allchive.server.core.consts.AllchiveConst.BEARER;
import static allchive.server.core.consts.AllchiveConst.KAKAO_OAUTH_QUERY_STRING;

import allchive.server.api.auth.model.dto.OauthUserInfoDto;
import allchive.server.core.annotation.Helper;
import allchive.server.core.dto.OIDCDecodePayload;
import allchive.server.core.properties.KakaoOAuthProperties;
import allchive.server.domain.domains.user.domain.enums.OauthInfo;
import allchive.server.domain.domains.user.domain.enums.OauthProvider;
import allchive.server.infrastructure.oauth.kakao.client.KakaoInfoClient;
import allchive.server.infrastructure.oauth.kakao.client.KakaoOauthClient;
import allchive.server.infrastructure.oauth.kakao.dto.KakaoInformationResponse;
import allchive.server.infrastructure.oauth.kakao.dto.KakaoTokenResponse;
import allchive.server.infrastructure.oauth.kakao.dto.KakaoUnlinkTarget;
import allchive.server.infrastructure.oauth.kakao.dto.OIDCPublicKeysResponse;
Expand Down Expand Up @@ -86,4 +89,11 @@ public void withdrawKakaoOauthUser(String oid) {
unlinkKaKaoTarget.getAud());
kakaoInfoClient.unlinkUser(header, unlinkKaKaoTarget);
}

/** 유저 정보 가져오기 * */
public OauthUserInfoDto getUserInfo(String oauthAccessToken) {
KakaoInformationResponse response =
kakaoInfoClient.kakaoUserInfo(BEARER + oauthAccessToken);
return OauthUserInfoDto.fromKakao(response);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package allchive.server.api.auth.service.helper;


import allchive.server.api.auth.model.dto.OauthUserInfoDto;
import allchive.server.api.auth.model.dto.response.OauthLoginLinkResponse;
import allchive.server.api.auth.model.dto.response.OauthTokenResponse;
import allchive.server.core.annotation.Helper;
Expand Down Expand Up @@ -81,6 +82,17 @@ public void withdraw(OauthProvider provider, String oid, String appleAccessToken
break;
case APPLE:
appleOauthHelper.withdrawAppleOauthUser(appleAccessToken);
break;
default:
throw InvalidOauthProviderException.EXCEPTION;
}
}

/** 유저 정보 가져오기 * */
public OauthUserInfoDto getUserInfo(OauthProvider provider, String oauthAccessToken) {
switch (provider) {
case KAKAO:
return kakaoOauthHelper.getUserInfo(oauthAccessToken);
default:
throw InvalidOauthProviderException.EXCEPTION;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package allchive.server.api.common.util;


import allchive.server.core.error.exception.EmptyParamValueException;
import org.springframework.stereotype.Component;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ public class User extends BaseTimeEntity {

@Builder
private User(
String name,
String email,
String nickname,
String profileImgUrl,
List<Category> categoryList,
OauthInfo oauthInfo) {
this.name = name;
this.email = email;
this.nickname = nickname;
this.profileImgUrl = profileImgUrl;
this.categories = categoryList;
Expand All @@ -63,11 +67,15 @@ private User(
}

public static User of(
String name,
String email,
String nickname,
String profileImgUrl,
List<Category> categoryList,
OauthInfo oauthInfo) {
return User.builder()
.name(name)
.email(email)
.nickname(nickname)
.profileImgUrl(profileImgUrl)
.categoryList(categoryList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ public Boolean checkUserCanLogin(OauthInfo oauthInfo) {

@Transactional
public User registerUser(
String name,
String email,
String nickname,
String profileImgUrl,
List<Category> categoryList,
OauthInfo oauthInfo) {
userValidator.validUserCanRegister(oauthInfo);
final User newUser = User.of(nickname, profileImgUrl, categoryList, oauthInfo);
final User newUser = User.of(name, email, nickname, profileImgUrl, categoryList, oauthInfo);
userAdaptor.save(newUser);
return newUser;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@


import allchive.server.infrastructure.oauth.kakao.config.KakaoInfoConfig;
import allchive.server.infrastructure.oauth.kakao.dto.KakaoInformationResponse;
import allchive.server.infrastructure.oauth.kakao.dto.KakaoUnlinkTarget;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;

Expand All @@ -16,4 +18,7 @@ public interface KakaoInfoClient {
@PostMapping(path = "/v1/user/unlink", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
void unlinkUser(
@RequestHeader("Authorization") String adminKey, KakaoUnlinkTarget unlinkKaKaoTarget);

@GetMapping("/v2/user/me")
KakaoInformationResponse kakaoUserInfo(@RequestHeader("Authorization") String accessToken);
}
Loading

0 comments on commit f256d45

Please sign in to comment.