Skip to content

Commit

Permalink
fix: 애플로그인 기존 유저 검증 로직 수정 - socialType 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
daeunkwak committed Aug 26, 2023
1 parent 3b7c083 commit 2fc901f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ public interface MemberRepository extends JpaRepository<Member, Long>, MemberCus

@Query("select m from Member m where m.socialType =:socialType and m.socialId =:socialId")
Optional<Member> findByMemberBySocialTypeAndSocialId(SocialType socialType, String socialId);

@Query("select m from Member m where m.email =:email and m.socialType =:APPLE")
Optional<Member> findByEmailAndSocialType(String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
.orElseThrow(() -> new MemberTokenNotFoundException("해당 리프레시 토큰이 존재하지 않습니다."));

final LocalDateTime refreshTokenExpirationTime = memberToken.getTokenExpirationTime();
log.info("refreshTokenExpirationTime :::: " + refreshTokenExpirationTime);

//refresh token 만료 전이면 access token 재발급 및 Authorization Header 세팅
if (!tokenProvider.isTokenExpired(LocalDateTime.now(), refreshTokenExpirationTime)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,35 +153,38 @@ public ResponseJwtTokenDto loginAppleIos(String tokenString) throws JsonProcessi
JsonObject userInfoObject = (JsonObject) JsonParser.parseString(jsonString);

JsonElement appleAlg = userInfoObject.get("email");
log.info("getEmail ::::::::: " + userInfoObject.get("email"));
String email = appleAlg.getAsString();
log.info("email ::::: " + email);

OAuthAttributes socialUserInfo = OAuthAttributes
.builder()
.email(email) // 이메일 동의 x 경우
.email(email)
.name("")
.socialType(SocialType.APPLE)
.build();

log.info("oauthAttributes: {}", socialUserInfo.toString());

// 첫 가입인 경우
Optional<Member> findMember = memberRepository.findByEmail(socialUserInfo.getEmail());
Optional<Member> findMember = memberRepository.findByEmailAndSocialType(socialUserInfo.getEmail());

if(findMember.isEmpty()){
String socialId = (String) userInfo.get("sub");
log.info("처음 가입한 유저 socialId ::::::::::::::::: " + socialId);
log.info("처음 가입한 유저 email ::::::::::::::::: " + email);

Member newMember = Member.createApple(email, SocialType.APPLE, socialId);

Member newMember = Member.createApple(socialUserInfo.getEmail(), SocialType.APPLE, socialId);
newMember.updateProfileImage();
requestMember = memberRepository.save(newMember);

return generateTokenKakao(requestMember);
return generateTokenApple(requestMember);
}

// 이미 가입한 경우
String socialId = (String) userInfo.get("sub");
log.info("이미 가입한 유저 socialId ::::::::::::::::: " + socialId);
log.info("이미 가입한 유저 email ::::::::::::::::: " + email);

requestMember = memberRepository.findByMemberBySocialTypeAndSocialId(SocialType.APPLE, socialId).orElseThrow(() -> {
throw new CiderException("유저를 찾을 수 없습니다.");
Expand All @@ -190,30 +193,30 @@ public ResponseJwtTokenDto loginAppleIos(String tokenString) throws JsonProcessi
return generateTokenNotNew(requestMember);
}

public ResponseJwtTokenDto generateTokenApple(Member member) {

//String socialId = (String) claims.get("sub");

// JWT 토큰 생성
TokenDto tokenDto = tokenProvider.createTokenDtoApple(member.getMemberId());
log.info("tokenDto: {}", tokenDto);

ResponseJwtTokenDto responseJwtTokenDto = modelMapper.map(tokenDto, ResponseJwtTokenDto.class);
final boolean isNewMember = StringUtils.isEmpty(member.getMemberName());
responseJwtTokenDto.setIsNewMember(isNewMember);
if (!isNewMember) {
responseJwtTokenDto.setMemberName(member.getMemberName());
}
responseJwtTokenDto.setMemberId(member.getMemberId());
responseJwtTokenDto.setMemberName("");
responseJwtTokenDto.setBirthday("");
responseJwtTokenDto.setGender("");

return responseJwtTokenDto;
}
// public ResponseJwtTokenDto generateTokenApple(Member member) {
//
// //String socialId = (String) claims.get("sub");
//
// // JWT 토큰 생성
// TokenDto tokenDto = tokenProvider.createTokenDtoApple(member.getMemberId());
// log.info("tokenDto: {}", tokenDto);
//
// ResponseJwtTokenDto responseJwtTokenDto = modelMapper.map(tokenDto, ResponseJwtTokenDto.class);
// final boolean isNewMember = StringUtils.isEmpty(member.getMemberName());
// responseJwtTokenDto.setIsNewMember(isNewMember);
// if (!isNewMember) {
// responseJwtTokenDto.setMemberName(member.getMemberName());
// }
// responseJwtTokenDto.setMemberId(member.getMemberId());
// responseJwtTokenDto.setMemberName("");
// responseJwtTokenDto.setBirthday("");
// responseJwtTokenDto.setGender("");
//
// return responseJwtTokenDto;
// }


public ResponseJwtTokenDto generateTokenKakao(Member member) {
public ResponseJwtTokenDto generateTokenApple(Member member) {

// JWT 토큰 생성
TokenDto tokenDto = tokenProvider.createTokenDtoKakao(member.getMemberId());
Expand Down Expand Up @@ -300,6 +303,9 @@ public PublicKey getPublicKey(JsonElement kid, JsonElement alg) {

// 로그아웃
public void logout(String refreshToken, LocalDateTime now) {

// List<MemberToken> memberTokenList = memberTokenRepository.findByMember

final MemberToken memberToken = memberTokenRepository.findByRefreshToken(refreshToken)
.orElseThrow(() -> new MemberTokenNotFoundException("해당 리프레시 토큰이 존재하지 않습니다."));
memberToken.expire(now);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public TokenDto createTokenDtoApple(Long memberId) {
String accessToken = createAccessToken(memberId, accessTokenExpireTime);
String refreshToken = createRefreshToken(memberId, refreshTokenExpireTime);

final MemberToken memberToken = MemberToken.create(refreshToken, DateTimeUtils.convertToLocalDateTime(refreshTokenExpireTime), memberId);
final MemberToken memberToken = MemberToken.create(accessToken, refreshToken, DateTimeUtils.convertToLocalDateTime(refreshTokenExpireTime), memberId);
memberTokenRepository.save(memberToken);

return TokenDto.builder()
Expand All @@ -116,7 +116,7 @@ public TokenDto createTokenDtoKakao(Long memberId) {
String accessToken = createAccessTokenV2(memberId, accessTokenExpireTime);
String refreshToken = createRefreshTokenV2(memberId, refreshTokenExpireTime);

final MemberToken memberToken = MemberToken.create(refreshToken, DateTimeUtils.convertToLocalDateTime(refreshTokenExpireTime), memberId);
final MemberToken memberToken = MemberToken.create(accessToken, refreshToken, DateTimeUtils.convertToLocalDateTime(refreshTokenExpireTime), memberId);
memberTokenRepository.save(memberToken);

return TokenDto.builder()
Expand Down
2 changes: 1 addition & 1 deletion cider-domain/src/main/java/com/cmc/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static Member createApple(String email, SocialType socialType, String soc

return Member.builder()
.memberName("")
.email("")
.email(email)
.memberBirth("")
.memberGender("")
.memberLevel(new MemberLevel(1, "시작 챌린저", 0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ public class MemberToken extends BaseTimeEntity {

private String refreshToken;

private String accessToken;

private LocalDateTime tokenExpirationTime;

private Long memberId;

public static MemberToken create(String refreshToken, LocalDateTime tokenExpiredTime, Long memberId) {
public static MemberToken create(String accessToken, String refreshToken, LocalDateTime tokenExpiredTime, Long memberId) {
final MemberToken memberToken = MemberToken.builder()
.accessToken(accessToken)
.refreshToken(refreshToken)
.tokenExpirationTime(tokenExpiredTime)
.memberId(memberId)
Expand Down

0 comments on commit 2fc901f

Please sign in to comment.