-
Notifications
You must be signed in to change notification settings - Fork 112
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
경북대 BE_김은선 5주차 과제 (1단계) #339
경북대 BE_김은선 5주차 과제 (1단계) #339
Conversation
…xtraction from token
테스트 코드에 주석메세지 추가
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1주차에서 카카오 토큰 가져와서 유저 정보 로그인하는 방식 확인했습니다
5주차 1단계 고생하셨습니다
리뷰드린 내용은 2단계에 반영해주세요!
@GetMapping("/kakao/login") | ||
public String login(Model model) { | ||
model.addAttribute("kakaoClientId", kakaoProperties.clientId()); | ||
model.addAttribute("kakaoRedirectUrl", kakaoProperties.redirectUrl()); | ||
return "kakaoLogin"; | ||
} | ||
|
||
@GetMapping("/kakao/oauth2/callback") | ||
public String callbackKakao(@RequestParam String code, Model model) { | ||
|
||
try { | ||
String accessToken = kakaoService.login(code); | ||
model.addAttribute("accessToken", accessToken); | ||
return "home"; | ||
} catch (MemberNotFoundException e) { | ||
model.addAttribute("member", new Member()); // Member 객체 추가 | ||
return "register"; | ||
} | ||
|
||
} | ||
|
||
@GetMapping("/kakao/loginSuccess") | ||
public String loginSuccess() { | ||
return "kakaoLoginSuccess"; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게 전부 kakao를 prefix path로 사용한다면 컨트롤러 자체에 path 등록해두는게 좋지 않을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵 @RequestMapping("/kakao") 로 따로 뺐습니다!
Map<String, String> response = restTemplate.postForObject(url, null, HashMap.class); | ||
|
||
return response.get("access_token"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
토큰을 이런식으로 빼셨군요 👍🏼
public String login(String code) throws MemberNotFoundException { | ||
String accessToken = getKakaoAccessToken(code); | ||
JSONObject userInfo = getUserInfo(accessToken); | ||
|
||
String email= null; | ||
if (userInfo.has("kakao_account")) { | ||
JSONObject kakaoAccount = userInfo.getJSONObject("kakao_account"); | ||
if (kakaoAccount.has("email")) { | ||
email = kakaoAccount.getString("email"); | ||
} | ||
} | ||
|
||
if (email == null) { | ||
throw new MemberNotFoundException("Email not found"); | ||
} | ||
// 로그인 성공 처리 | ||
return accessToken; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getKakaoAccessToken에서 리프레시토큰은 따로 가져오지 않아도 괜찮을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
토큰 두 개 모두 받아오는 객체를 새로 정의해서 받아오도록 수정했습니다!
@Test | ||
public void testLogin() { | ||
String url = "http://localhost:" + port + "/kakao/login"; | ||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); | ||
|
||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); | ||
} | ||
|
||
// 유효한 코드로 카카오 로그인을 시도 | ||
@Test | ||
public void testCallbackKakaoSuccess() { | ||
String code = "valid_code"; | ||
String accessToken = "valid_access_token"; | ||
|
||
when(kakaoService.login(code)).thenReturn(accessToken); | ||
|
||
String url = "http://localhost:" + port + "/kakao/oauth2/callback?code=" + code; | ||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); | ||
|
||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); | ||
} | ||
|
||
// 회원을 찾을 수 없는 경우 | ||
@Test | ||
public void testCallbackKakaoMemberNotFound() { | ||
String code = "valid_code"; | ||
|
||
when(kakaoService.login(code)).thenThrow(MemberNotFoundException.class); // 메시지 없이 예외 타입만 설정 | ||
|
||
String url = "http://localhost:" + port + "/kakao/oauth2/callback?code=" + code; | ||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); | ||
|
||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); | ||
assertThat(response.getBody()).contains("register"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
로그인 테스트 👍🏼
@Test | ||
void test1(){ | ||
var url = "https://kauth.kakao.com/oauth/token"; | ||
var code = "MZqnpa_Ch0moTrUURmm4PEi74CgD3sYdPdG1cJU4nnv4ilOioKXq5AAAAAQKPCQfAAABkPMfVqjNsk3jZ7dWzg"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 코드가 여기 있어도 괜찮을까요?
최소한 properties로 빼서 관리되면 좋을 것 같아요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵 수정하고 서비스 내에서도 url 사용하도록 바꿨습니다!
안녕하세요 멘토님,
리뷰요청드립니다!
1단계 진행사항입니다