Skip to content

Commit

Permalink
Fix DIG-160: 카카오 소셜 Exception 추가
Browse files Browse the repository at this point in the history
### 카카오 소셜 친구목록 받아오기 테스트
  • Loading branch information
Dongjin113 committed Nov 23, 2023
1 parent e165805 commit 02359ab
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum ErrorCode implements ErrorType {
USER_NOT_AUTHORIZED_JOURNAL(HttpStatus.FORBIDDEN, "403", "운동일지에 접근 권한이 없습니다"),
UNAUTHORIZED_USER_ACCESS(HttpStatus.FORBIDDEN, "403", "접근 권한이 부족합니다."),
WRONG_APPROACH(HttpStatus.FORBIDDEN, "403", "잘못된 접근입니다"),
FORBIDDEN_KAKAO_SOCIAL(HttpStatus.FORBIDDEN,"403","카카오 정보동의가 필요합니다"),

//NOT_FOUND
NOT_FOUNT_USER_AUTHENTICATION(HttpStatus.NOT_FOUND,"404","유저 인증정보를 찾을 수 없습니다"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ogjg.daitgym.common.exception.user;

import com.ogjg.daitgym.common.exception.CustomException;
import com.ogjg.daitgym.common.exception.ErrorCode;
import com.ogjg.daitgym.common.exception.ErrorData;

public class ForbiddenKaKaoSocial extends CustomException {
public ForbiddenKaKaoSocial() {
super(ErrorCode.FORBIDDEN_KAKAO_SOCIAL);
}

public ForbiddenKaKaoSocial(String message) {
super(ErrorCode.FORBIDDEN_KAKAO_SOCIAL, message);
}

public ForbiddenKaKaoSocial(ErrorData errorData) {
super(ErrorCode.FORBIDDEN_KAKAO_SOCIAL, errorData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class JwtUtils {

private static final int DAY = HOUR * 24;

private static final int ACCESS_TOKEN_VALID_TIME = 30000;
private static final int ACCESS_TOKEN_VALID_TIME = MINUTE * 5;

private static final long REFRESH_TOKEN_VALID_TIME = DAY * 30L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/user/kakao")
@RequestMapping("/api/users/kakao")
@RequiredArgsConstructor
public class UserFriendController {

Expand Down
50 changes: 29 additions & 21 deletions src/main/java/com/ogjg/daitgym/user/service/KakaoFriendService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.ogjg.daitgym.user.service;

import com.ogjg.daitgym.common.exception.user.ForbiddenKaKaoSocial;
import com.ogjg.daitgym.common.exception.user.NotFoundUser;
import com.ogjg.daitgym.common.exception.user.NotFoundUserAuthentication;
import com.ogjg.daitgym.domain.UserAuthentication;
import com.ogjg.daitgym.user.dto.response.KaKaoFriendsResponse;
import com.ogjg.daitgym.user.repository.UserAuthenticationRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service
public class KakaoFriendService {

Expand All @@ -29,33 +33,37 @@ public KakaoFriendService(UserAuthenticationRepository userAuthenticationReposit
*/
@Transactional
public KaKaoFriendsResponse requestKaKaoFriendsList(String email) {
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + getAccessToken(email));
headers.setContentType(MediaType.APPLICATION_JSON);
try {
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + getAccessToken(email));
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<String> entity = new HttpEntity<>(headers);
HttpEntity<String> entity = new HttpEntity<>(headers);

ResponseEntity<KaKaoFriendsResponse> response = restTemplate.exchange(
"https://kapi.kakao.com/v1/api/talk/friends",
HttpMethod.GET,
entity,
KaKaoFriendsResponse.class
);
ResponseEntity<KaKaoFriendsResponse> response = restTemplate.exchange(
"https://kapi.kakao.com/v1/api/talk/friends",
HttpMethod.GET,
entity,
KaKaoFriendsResponse.class
);

KaKaoFriendsResponse kaKaoFriendsResponse = response.getBody();
KaKaoFriendsResponse kaKaoFriendsResponse = response.getBody();

kaKaoFriendsResponse.getElements().forEach(
kaKaoFriendResponseDto -> {
UserAuthentication userAuthentication = userAuthenticationRepository.findByProviderId(kaKaoFriendResponseDto.getId())
.orElseThrow(NotFoundUserAuthentication::new);
kaKaoFriendsResponse.getElements().forEach(
kaKaoFriendResponseDto -> {
UserAuthentication userAuthentication = userAuthenticationRepository.findByProviderId(kaKaoFriendResponseDto.getId())
.orElseThrow(NotFoundUserAuthentication::new);

kaKaoFriendResponseDto.putUserData(
userAuthentication.getUser().getNickname(),
userAuthentication.getUser().getImageUrl()
);
});
kaKaoFriendResponseDto.putUserData(
userAuthentication.getUser().getNickname(),
userAuthentication.getUser().getImageUrl()
);
});

return kaKaoFriendsResponse;
return kaKaoFriendsResponse;
} catch (HttpClientErrorException.Forbidden e) {
throw new ForbiddenKaKaoSocial();
}
}

private String getAccessToken(String email) {
Expand Down

0 comments on commit 02359ab

Please sign in to comment.