Skip to content

Commit

Permalink
Merge pull request #21 from kakao-asset/feature/#17-add-efk-log
Browse files Browse the repository at this point in the history
hoxfix: 이메일 없을 시 에러 처리
  • Loading branch information
ur2e committed Dec 10, 2022
2 parents dfc22de + 6f5068d commit 507d18a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequiredArgsConstructor
Expand All @@ -22,13 +24,10 @@ public ResponseEntity<BasicResponse> login(@RequestParam("code") String code) {
// 프론트 넘어온 인가 코드를 통해 kakao access token 발급
OauthToken oauthToken = authService.getKakaoAccessToken(code);

// kakao access token으로 DB에 멤버 저장 및 JWT Token 발급
LoginDto loginDto = authService.saveMemberAndGetJwtToken(oauthToken.getAccess_token());

BasicResponse response = BasicResponse.builder()
.code(HttpStatus.OK.value())
.message("로그인에 성공했습니다.")
.data(loginDto)
.data(authService.saveMemberAndGetJwtToken(oauthToken.getAccess_token()))
.build();

return new ResponseEntity<>(response, HttpStatus.OK);
Expand Down Expand Up @@ -73,27 +72,5 @@ public ResponseEntity<BasicResponse> unlink(@PathVariable("userId") Long userId)

return new ResponseEntity<>(response, HttpStatus.OK);
}


@GetMapping("/bridge")
public ResponseEntity<BasicResponse> moveMain(@RequestHeader String token){
if(authService.isValidToken(token)){
BasicResponse response = BasicResponse.builder()
.code(HttpStatus.OK.value())
.message("유효한 토큰입니다.")
.data(Collections.emptyList())
.build();

return new ResponseEntity<>(response, HttpStatus.OK);
} else {
BasicResponse response = BasicResponse.builder()
.code(HttpStatus.UNAUTHORIZED.value())
.message("유효하지 않은 토큰입니다.")
.data(Collections.emptyList())
.build();

return new ResponseEntity<>(response, HttpStatus.UNAUTHORIZED);
}
}
}

35 changes: 30 additions & 5 deletions src/main/java/com/kakaoasset/portfolio/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.concurrent.TimeUnit;

@Service
Expand Down Expand Up @@ -79,10 +77,37 @@ public OauthToken getKakaoAccessToken(String code) {
return oauthToken;
}

public LoginDto saveMemberAndGetJwtToken(String token) {
public Object saveMemberAndGetJwtToken(String token) {

KakaoProfile profile = findProfile(token);

if(profile.getKakao_account().getEmail() == null){
System.out.println(profile.getId());
RestTemplate rt = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Bearer " + token);
headers.add("Content-type", "application/x-www-form-urlencoded;charset=utf-8");

HttpEntity<MultiValueMap<String, String>> kakaoProfileRequest =
new HttpEntity<>(headers);
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("target_id_type", "user_id");
params.add("target_id", String.valueOf(profile.getId()));
ResponseEntity<String> unlinkResponse = rt.exchange(
"https://kapi.kakao.com/v1/user/unlink",
HttpMethod.POST,
new HttpEntity<>(params, headers),
String.class
);

return BasicResponse.builder()
.code(HttpStatus.UNAUTHORIZED.value())
.message("이메일을 넣어주세요")
.data(Collections.emptyList())
.build();
}

Member member = memberRepository.findByEmail(profile.getKakao_account().getEmail());

if(member == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public Object selectNewsStock(String stockCode) {
}
}

System.out.println("test");

return jsonarr.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ public String selectSectorStock(String stock_sector){

jsonarr.put(temp);
}

return jsonarr.toString();
}

Expand Down

0 comments on commit 507d18a

Please sign in to comment.